Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I used multiple If's in my code and it one of them is not working. Can someone show me where I do wrong here?

I need to make those "If’s" below work together ("If" inputs’s value less than 1900, I need message display "Please enter a valid year" and "If" Dog’s age higher than 14, I need message display as "Your dog completed its life span" message at third sentance. However only one them works and sometimes other one. But not working together. Can someone show me where I do wrong here? thank you.

function calc() {
    let x = document.getElementById("year").value
    let current = new Date().getFullYear()
    let age = current - Number(x)
    let trans = age * 7
    let remain = 15 - age
    let msg ="Your dog is " + age + " years old. Your dog's human race is " + trans + ". Your dog has " + remain + " years to complete its life span."
    document.getElementById("result").innerHTML= msg

    if (x < 1900) {
        document.getElementById("result").innerHTML="Please enter a valid year." 
    }

    if (remain <= 0) {
        document.getElementById("result").innerHTML= "Your dog is " + age + " years old. Your dog's human race is " + trans + ". Your dog completed its life span."
    }
}
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>New Page</title>
</head>
<body>

    <h1>Dog Age Calculate</h1>
    <b>Please enter your dog's birth of year:</b><br><br>
    <input type="number" id="year" required min="1900" max="2022"><br><br>
    <button onclick="calc()">Calculate</button><br>
    <p id="result"></p>

<script src="Chasm.js"></script>
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are not leaving your function when your if case is triggered. Therefore it is possible that after the first if the second ifis triggered as well.

You have a few options here.

Option 1:

return from the function if any if case is triggered.

    if (x < 1900) {
        return document.getElementById("result").innerHTML="Please enter a valid year." 
    }

    if (remain <= 0) {
        return document.getElementById("result").innerHTML= "Your dog is " + age + " years old. Your dog's human race is " + trans + ". Your dog completed its life span."
    }

    //set the msg at the end of the function so you are checking your if statements before
    document.getElementById("result").innerHTML= msg

Option 2:

use if / else

    if (x < 1900) {
        document.getElementById("result").innerHTML="Please enter a valid year." 
    } else if (remain <= 0) {
        document.getElementById("result").innerHTML= "Your dog is " + age + " years old. Your dog's human race is " + trans + ". Your dog completed its life span."
    } else {
        document.getElementById("result").innerHTML= msg
    }

You could also improve performance when setting the first if directly under your assigned variable. If that case matches, you don’t need the further calculations.

let x = document.getElementById("year").value
if (x < 1900) {
    return document.getElementById("result").innerHTML="Please enter a valid year." 
}
....
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading