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

КОМЕНТАРІ • 49

  • @TheDodoKing1239-wi8oc
    @TheDodoKing1239-wi8oc Рік тому +1

    I am new to this channel but the tutorial worked perfectly! Thanks!

  • @heliasyusuf2742
    @heliasyusuf2742 3 роки тому +3

    you earned a new student enrolment from Nigeria! thanks, my assignment is clearer

  • @ChelseyTschida
    @ChelseyTschida 3 роки тому +7

    Just stumbled onto this (needed help with an assignment). This is fantastic, and it really helped me to understand what is happening! Thank you!

    • @WorldClassTechEd
      @WorldClassTechEd  3 роки тому +3

      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! :-)

  • @srinivasaraoyp3640
    @srinivasaraoyp3640 3 роки тому +2

    Your explanation is fantastic. Very simple and to the point. Great. Thank you.

  • @Explorewithlarry
    @Explorewithlarry Рік тому +1

    I enjoyed every second of your explanations!

  • @solechino
    @solechino Рік тому +1

    WOW this was fantastic!

  • @violetdominikovich207
    @violetdominikovich207 2 роки тому +1

    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?

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому

      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.

  • @chesteraltiery1962
    @chesteraltiery1962 2 роки тому +1

    What if we want to pick the random number and the computer to guess the number?

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому

      Sure. You could do this. But I have a question. If you are picking the "random" number is it really random?

  • @adamburgess3949
    @adamburgess3949 3 роки тому +1

    Great video! Thank you for posting!

  • @azkiafairuz4801
    @azkiafairuz4801 3 роки тому +1

    Thanks grandma you teaching me for free

    • @WorldClassTechEd
      @WorldClassTechEd  3 роки тому

      You are welcome. I'm glad to hear you are learning something.

  • @chelsealove8324
    @chelsealove8324 3 роки тому +1

    Great instructions. Thank you!

  • @konami2839
    @konami2839 2 роки тому +1

    Can you use while loop instead of clicking "play game" like while guess != secretNum

  • @ol1175
    @ol1175 3 роки тому +1

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

    • @WorldClassTechEd
      @WorldClassTechEd  3 роки тому +1

      You need to use the parseInt() function. It will turn the input from prompt (which is always a string), into a number. :-)

  • @pavankumar-hb1sc
    @pavankumar-hb1sc 3 роки тому +1

    Can any one know how to detect random numbers many colour prediction website programming running with java script they give random numbers

  • @learnwithdounia6946
    @learnwithdounia6946 3 роки тому +2

    thank u,
    i also make video about Guessing number game using android studio, hope u see it

  • @RealGameTitan
    @RealGameTitan 3 роки тому +1

    Nice exercise!

  • @silentxcure
    @silentxcure 3 роки тому +1

    sorry but you deserve more views! subscribing!

  • @kaarls-anthems
    @kaarls-anthems 3 роки тому +1

    what will you use program?

    • @WorldClassTechEd
      @WorldClassTechEd  3 роки тому +2

      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!

  • @amrmusharrafa4914
    @amrmusharrafa4914 3 роки тому +1

    good job

  • @katrinamalone5611
    @katrinamalone5611 3 роки тому

    I am currently working a number guessing assignment and it is not executing for me. Any suggestions on how to correct?

    • @WorldClassTechEd
      @WorldClassTechEd  3 роки тому +1

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

  • @samueldias2986
    @samueldias2986 2 роки тому +1

    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.

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому +1

      But, you don't want to have to refresh the page everytime.

    • @samueldias2986
      @samueldias2986 2 роки тому +1

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

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому

      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.

  • @marie34777
    @marie34777 2 роки тому

    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");
    }
    }

    • @marie34777
      @marie34777 2 роки тому +1

      the errors are that the console, prompt and window is not defined.(JS Hint)

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому

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

    • @marie34777
      @marie34777 2 роки тому +1

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

    • @WorldClassTechEd
      @WorldClassTechEd  2 роки тому

      Great!!