Codeguy Posted January 9, 2016 Share Posted January 9, 2016 <html> <head> <title> Javascript opdrachten </title> <script type="text/javaScript"> var leeftijd = window.prompt("Wat is jouw leeftijd"); if (leeftijd <= 1) { document.write("Je bent een baby"); } else if (leeftijd >= 1 && <=3) { document.write("Je bent een kleuter"); } else if (leeftijd >= 6 && <=12) { document.write("Je bent een kind"); } else if (leeftijd >= 12 && <=16) { document.write("Je bent een puber"); } else if (leeftijd >=16 && <=21) { document.write("Je bent een adolescent"); } else if (leeftijd >=21) { document.write("Je bent een volwassenen"); } else (leeftijd <=0 && >=110) { document.write("Verkeerde invoer"); } </script> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
Stvsynrj Posted January 10, 2016 Share Posted January 10, 2016 hi, you cant do that : if (x>0 && x<=3) .... else if (x >=3 && x<6) ... as you can see, if you put 3, the 2 ifs will be called and only the second one will work.it should be : if (x>0 && x<=3) ..... else if (x>3 && x<=6) .... and so on .... Quote Link to comment Share on other sites More sharing options...
AzraelTycka Posted January 10, 2016 Share Posted January 10, 2016 Hello, @StvsynrjThat's not true, that's not how JS works.JS conditions go one by one until one is successful then it does NOT continue through if/else if/else structure.Simple example in jsfiddle for you to see. Here is the code from fiddle similar to your code, this is perfectly valid code for JS and only one console.log will be logged and that's the first one in this case.var a = 3;if (a <= 3){console.log('first');}else if(a >= 3){console.log('second');}@CodeguyYour problem is with your conditions, in JS you can't NOT specify variable in condition as you do so adjust them this way:your (leeftijd >= 1 && <=3) has to be ( (leeftijd >= 1) && (leeftijd <= 3) ) and your other conditions in similar way.And your else (leeftijd <=0 && >=110) is also a wrong syntax, else means do this if no other condition from before has been met thus there is supposed to be no condition, change it to just else {//your code here}.Your code working in jsfiddle with adjustments I mentioned (jsfiddle doesn't support document.write so I exchanged your document.write for console.log so don't forget to open your console ;-)). Quote Link to comment Share on other sites More sharing options...
Stvsynrj Posted January 10, 2016 Share Posted January 10, 2016 Well ok, it works, but the logic itself is broken. Quote Link to comment Share on other sites More sharing options...
AzraelTycka Posted January 10, 2016 Share Posted January 10, 2016 @Stvsynrj I don't think so and neither JS standards. It even makes execution of if conditions faster and gives you an opportunity to do some "tricks" which make for lower file sizes, and so on. Quote Link to comment Share on other sites More sharing options...
marcgfx Posted January 16, 2016 Share Posted January 16, 2016 well it's horrible code if I may say so... maybe this? not tested var state = {1:"baby",3:"small child",6:"child",....} function getAgeState(myAge){ for(var age in state){ if(myAge<=age) return state[age]; } return "oh"; } //and if you have to do it as above var a = 3; if (a <= 3){ console.log('first'); }else if(a >= 3){ console.log('second'); } //is the same as if(a<=3){ console.log('first'); }else{ console.log('second'); } //is the same as console.log(a<=3 ? 'first' : 'second'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.