Jump to content

Code not working what's wrong with it?


Codeguy
 Share

Recommended Posts

<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>

Link to comment
Share on other sites

Hello,

 

@Stvsynrj

That'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');}

@Codeguy

Your 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 ;-)).

Link to comment
Share on other sites

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');

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...