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

Irfan Hussain
PLUS
Irfan Hussain
Courses Plus Student 6,593 Points

2 Prompt Messages with if statement

// I wanna use 2 prompt message in if statement, if user not guess the correct number in 1st attempt, program run again and ask the question. Does it possible without loop.

var guess = prompt("Please guess the number between 1 to 6."); var randomNumber = Math.floor(Math.random() * 6) + 1; if (parseInt(guess) === randomNumber) { document.write("Congratulation, you guess the number"); }

else if (parseInt(guess) === randomNumber) { document.write("Congratulation, you guess the number"); }

else { document.write("Try again, number was " + randomNumber); }

Simon Coates
Simon Coates
28,694 Points

you'd usually use a loop (while, do while) to do this. I'm uncertain why you don't want to use a loop. Otherwise, you just repeat your code (or use a function to bundle the code to repeat) and use some conditional statements. (but this would be finite - the number of attempts would be equal to the number of times you repeat your code)

Irfan Hussain
Irfan Hussain
Courses Plus Student 6,593 Points

Thanks for your reply mates :) but i am very beginner in programming. So i was experimenting, maybe i could use to prompt messages with if statement.

2 Answers

Shane Oliver
Shane Oliver
19,977 Points

Having a program run again is the exact purpose of a loop, so here using a loop is best practice

Irfan Hussain
PLUS
Irfan Hussain
Courses Plus Student 6,593 Points

Thanks for your reply mates :) but i am very beginner in programming. So i was experimenting, maybe i could use to prompt messages with if statement.