Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

how to verify if a number is between 1 and 5

I have to verify if a number is between 1 and 5 (like if it was a dice). I'll be using a window.prompt... this is my code... what is wrong with it?

var resultatDe = parseInt(window.prompt("Entrer le résultat de dé obtenu")); if (resultatDe <= "5" && resultatDe >= "1") { window.alert = ("Vous avez obtenu un "+resultatDe); }else if (resultatDe == "6") { window.alert = ("Bravo, vous avez obtenu un "+resultatDe); }else { window.alert = ("Résultat du dé invalide"); }

thanks!

2 Answers

Hi Marie - you have a simple error - your 'if' statement is working on if your variable is greater equal to a string (as in "1") but instead it needs to be an integer as your are parsing the input from the user to a whole number.

Basically remove the quote marks (") around the numbers in your if statement

var resultatDe = parseInt(window.prompt("Entrer le résultat de dé obtenu")); 

if (resultatDe <= 5 && resultatDe >= 1) 
{ 
     window.alert = ("Vous avez obtenu un "+resultatDe); 
}
else if (resultatDe == 6) { 
    window.alert = ("Bravo, vous avez obtenu un "+resultatDe); 
} 
else
{ 
    window.alert = ("Résultat du dé invalide"); 
}

you are right since their are numbers now...

I try that!