Code a Number Guessing Game in Javascript
Вставка
- Опубліковано 26 сер 2024
- In this video you will learn how to create random numbers in Javascript, how to output to the console for debugging, how to collect user input and store it in a variable and how to compare the user input to the random number, using an if / else statement.
This tutorial is great for beginning programmers. You will learn how to use control structures (if statements) to control the flow of the program. You will also learn how to generate random numbers within a certain range of whole numbers. You will learn how to use Math.random() and Math.floor.
Javascript is a great language for beginning programmers. Check out my other videos on using Javascript to learn programming.
If you are a teacher, please feel free to use my videos as part of your curriculum. I have many more videos on my channel. Check them out. :-)
If this has proved helpful, please consider subscribing to my channel. Have a great day!!
I am new to this channel but the tutorial worked perfectly! Thanks!
Welcome
you earned a new student enrolment from Nigeria! thanks, my assignment is clearer
Wonderful. Welcome to the channel.
Just stumbled onto this (needed help with an assignment). This is fantastic, and it really helped me to understand what is happening! Thank you!
Great! I'm glad it helped. I'm a high school teacher and create these videos for my students....but I'm super happy when others can find them and use them. Merry Christmas! :-)
Your explanation is fantastic. Very simple and to the point. Great. Thank you.
You are welcome! Thanks for the feedback.
I enjoyed every second of your explanations!
Awesome, thank you!
WOW this was fantastic!
Glad you enjoyed it!
hi Kris, the video was so helpful!! I want to add a scoreboard to my code after each play to show what everyones score is and I was wondering if you knew how to do that?
That's a great idea! You have to use some kind of database to store the scores of everyone who has played the game. This is a much more advanced programming concept. It is beyond the scope of this video.
What if we want to pick the random number and the computer to guess the number?
Sure. You could do this. But I have a question. If you are picking the "random" number is it really random?
Great video! Thank you for posting!
You're welcome.
Thanks grandma you teaching me for free
You are welcome. I'm glad to hear you are learning something.
Great instructions. Thank you!
You're welcome. Glad it was helpful!
Can you use while loop instead of clicking "play game" like while guess != secretNum
Yes. You could do that.
Kris Thanks a lot.
i am wondering how can i put this in while loop, and when i type "quit" the loop stops.
Problem that myinput question is a number var input =+prompt(Guess a number) but trying to compare with Strings(
var correctAnswer = 10;
var input = + prompt("what is your version");
var score = 10;
while(input!=="quit"){
if(input===correctAnswer){
alert("correct");
break;
} else if (inputcorrectAnswer){
alert("too high");
score --;
console.log(score)
}
var input = + prompt("what is your version")
} alert("thanks your score is " + score);
You need to use the parseInt() function. It will turn the input from prompt (which is always a string), into a number. :-)
Can any one know how to detect random numbers many colour prediction website programming running with java script they give random numbers
Sorry. I've never tried that. Sounds interesting.
thank u,
i also make video about Guessing number game using android studio, hope u see it
I will check it out
Nice exercise!
Thanks!
sorry but you deserve more views! subscribing!
Haha. Thanks!
what will you use program?
I am using Brackets for writing the code. The language I'm writing in is Javascript. The program is an exercise for my high school students. Thanks!
good job
Thank you!
I am currently working a number guessing assignment and it is not executing for me. Any suggestions on how to correct?
Sorry. There's no way to know what the problem is without seeing the code. I suggest watching the video again and following the code example. Also check out the Javascript debugger by pressing F12 in the browser and addressing any error messages in the Console view. Good luck!!
IF YOU PUT THE randomNum OUTSIDE THE FUNCTION you will get onli 1 number generated by the computer.
it will not change unless you refresh the page.
But, you don't want to have to refresh the page everytime.
@@WorldClassTechEd true kkk.
I made the same game to run on cmd with node.
I didn´t find any video about that here.
just an idea kkk.
That's a great idea. I don't teach Node. But I'm glad you figured it out. Hey....maybe YOU could make a video about Node!! :-) That would be cool.
Hello Kris,
Thank you very much for your channel!
I typed the code in a js file in Brackets but I get a error code, the prompt box is working but the windows alert not.
Did I do something wrong?
function guessNumber(){
var randomNum = Math.floor(Math.random()* 51);
//output for the comment for debugging
console.log(randomNum);
//declare a variable
var guess;
guess = prompt("Please enter a number between and 50");
//console.log("Your guess is: " + guess);
if (guess < randomNum) {
window.alert("Your guess is too low");
} else if ( guess > randomNum){
window.alert("Your guess is too high");
} else if (guess == randomNum){
window.alert("You guessed it you win");
}
}
the errors are that the console, prompt and window is not defined.(JS Hint)
The errors that you listed above have no effect on the program. The console and window are built in...and don't need to be defined. But the debugger complains about it all the time. Does it work now??
@@WorldClassTechEd Thank you for your reply! I didn't see this message until now. It was in mij social box from gmail which I never use. It works!
Great!!