0

Please could you help me get the if statements in my javascript working?

Also i have put a link to a new page in my message variables, but i don't think it's quite right, any help on this would also be appreciated.

var messageLow = "Your results show that you currently have a low risk of developing diabetes. However, it is important that you maintain a healthy lifestyle in terms of diet and exercise.";
var messageMed = "Your results show that you currently have a medium risk of developing diabetes. For more information on your risk factors, and what to do about them, please visit our diabetes advice website at http://www.zha.org.zd/";
var messageHigh1 = "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your BMI. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh2 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your family history. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh3 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
 var messageHigh4 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your BMI and family history. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh5 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your BMI and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh6 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your family history and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a         href=contactForm.html>contact form</a>

 function displayMsg()
 {
    var age, bmi, family, sugar;
    age = document.querySelector('input[name = "age"]:checked').value;
    bmi = document.querySelector('input[name = "bmi"]:checked').value;
    family = document.querySelector('input[name = "family"]:checked').value;
    sugar = document.querySelector('input[name = "sugar"]:checked').value;

    var total = age + bmi + family + sugar;

    document.getElementById("container").innerHTML = "<b>Your result: </b><br>";

    if( total >= 0 && total <= 15)
    {
        document.getElementById("container").innerHTML += messageLow;

    }
    else if(total >= 16 && total <= 25)
    {
        document.getElementById("container").innerHTML += messageMed;

    }
    elseif (total > 25)
    {

        if (age=>10 && bmi =>10)
    }{
         document.getElementById("container").innerHTML += messageHigh1;
        }
    {
         if (age=>10 && family =>10)
     }{
        document.getElementById("container").innerHTML += messageHigh2;
        }
    {
        if (age=>10 && sugar =>10)
    }{
        document.getElementById("container").innerHTML += messageHigh3;
        }
    {
        if (bmi=>10 && family =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh4;
        }
    {
        if (bmi=>10 && sugar =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh5;
        }
    {   
        if (family=>10 && sugar =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh6;
        }
   else
    {
     break;
    }
}
Jiyl
  • 27
  • 5
  • 3
    can you put your code in the question? – Debabrata Mohanta Aug 05 '16 at 10:58
  • Yeah it's there now - – Jiyl Aug 05 '16 at 11:05
  • First thing I noticed: `else (total > 25) {` is wrong. `Else` can't have a condition. Either use `else if (total > 25) {` or `else {`. – Michel Aug 05 '16 at 11:10
  • ok thanks so it should look more like this? – Jiyl Aug 05 '16 at 11:11
  • also, do all my if statements need semi colons after them? (after the [arguments] in rounded brackets) – Jiyl Aug 05 '16 at 11:13
  • Reading your code, you should see tons of errors. I recommend some proper debugging, so you can see where it goes wrong. [This question](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) might provide some help with that. – Michel Aug 05 '16 at 11:15

1 Answers1

0

This is how you code should look like:

if( total >= 0 && total <= 15){

    document.getElementById("container").innerHTML += messageLow;

}else if(total >= 16 && total <= 25){

    document.getElementById("container").innerHTML += messageMed;

}else if(total > 25){

    if (age>=10 && bmi >=10){

      document.getElementById("container").innerHTML += messageHigh1;

    }else if(age>=10 && family >=10){

      document.getElementById("container").innerHTML += messageHigh2;

    }else if (age>=10 && sugar >=10) {

    document.getElementById("container").innerHTML += messageHigh3;

    }else if (bmi>=10 && family >=10){ 

    document.getElementById("container").innerHTML += messageHigh4;

    }else if (bmi>=10 && sugar >=10){  

    document.getElementById("container").innerHTML += messageHigh5;

    }else if (family>=10 && sugar >=10){ 

    document.getElementById("container").innerHTML += messageHigh6;

    }

}

Note that you only use break for loops or switch statements. You do not need to put ";" after if statements. It is called "condition", not arguments. Arguments are passed to functions. And you do not put ";" after the condition or the code after it(the one inside "{}") will always be executed even if the condition is not met;

is ">=" instead of "=>"

is "else if" not "elseif"

hope this helps you :)

Osagui Aghedo
  • 330
  • 4
  • 12