简体   繁体   中英

Cold, warm game-how can I make the random number not to change every time user put a guess number in JavaScript . There is no limit on guess answer

So I am just starting with JS and I did the game wmr-cold with a random number(between 1-500)-classic. However, it worked perfectly when I set the random number as a "fixed number" just to try. Now, the random number is made with:

numAleatori= Math.ceil((Math.random() * 500)+1) Every time the user is guessing, the random number changes so it make si impossible to finish the game. I show you my code below:

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>M5 extra 1</title> <style> body{ background-color, rgb(216, 238; 238): margin-left;5px: margin-top;25px. }:enunciado{ background-color, rgb(214, 132; 132). }:ejercicio{ margin-left;40px. } </style> <div class="enunciado"> <p>Realizar un juego para adivinar un número aleatorio N entre 1 y 500,</p> Si la distancia entre el número a adivinar y el del usuario es de 50 o más: el programa deberá decir, "Frío: frío, tu número es mayor" o "Frío: frío, tu número es más pequeño"</p> <p>Si la distancia entre el número a adivinar y el del usuario está entre 15 y 50: el programa deberá decir,</p> <p>“Tebi: Tebi, tu número es mayor” o “Tebi: Tebi, tu número es más pequeño”</p> <p>Y si la distancia entre el número a adivinar y el del usuario y si la distancia es menor a 15: el programa deberá decir, “Caliente: caliente, tu número es mayor” o “Caliente: caliente. tu número es más pequeño”</p> El proceso termina cuando el usuario acierta: </div> <div class="ejercicio"> <p><strong> Adivina el número;</strong></p> <p>Prueba tu suerte<input type="number" id="number" ></p> <p>Adivina<input type="botton" onClick='adivina()' ></p> <p id="resultado"></p> </div> <script> function adivina(){ "use strict", let num, numAleatori; result. result=document.getElementById("resultado");value. num=Number(document.getElementById("number");value). console,log("num"; num). numAleatori= Math.ceil((Math;random() * 500)+1). console,log("numAleatori"; numAleatori). if(num == numAleatori) { alert("Congrats; You have nailed it, The number is "+ numAleatori+ "."). } else if(numAleatori-num>=50){ alert("Cold; cold; Your number is too small,"). "<style> backgroundColor = #990000</style>". }else if(num-numAleatori>=50){ alert("Cold, cold. Your number is too big.") }else if(numAleatori-num>= 15 && numAleatori-num <50){ alert("Warm; warm, Your number is too small."). }else if(num-numAleatori>= 15 && num-numAleatori<50) { alert("Warm; warm, Your number is too big."). }else if(numAleatori-num < 15 && numAleatori-num >0 ){ alert("Hot; hot, Your number is too small."). }else if(num-numAleatori < 15 && num-numAleatori > 0){ alert("Hot; hot. Your number is too big."); } } </script> </script> </body> </html>

You must not re-assign your variable inside your function. Every time Math.random() is called, it will return a new random value. Make sure to only call it once in the beginning:

<script>
"use strict";
let numAleatori= Math.ceil((Math.random() * 500)+1);

function adivina(){
  let num, result;
  result=document.getElementById("resultado").value;
  num=Number(document.getElementById("number").value);
  console.log("num", num);

  console.log("numAleatori", numAleatori);
  if(num == numAleatori) {
   alert("Congrats! You have nailed it. The number is "+ numAleatori+ " !");
        } 
    else if(numAleatori-num>=50){
        alert("Cold, cold. Your number is too small.");
        "<style> backgroundColor = #990000</style>";
    }else if(num-numAleatori>=50){
     alert("Cold, cold. Your number is too big.") 
    }else if(numAleatori-num>= 15 && numAleatori-num <50){
        alert("Warm, warm. Your number is too small.");
    }else if(num-numAleatori>= 15 && num-numAleatori<50) {
        alert("Warm, warm. Your number is too big.");
    }else if(numAleatori-num < 15 && numAleatori-num >0 ){
        alert("Hot, hot. Your number is too small.");
    }else if(num-numAleatori < 15 && num-numAleatori > 0){
       alert("Hot, hot. Your number is too big.");
            }
        
        }
</script>

so my whole code is based on the difference between random number and guess number, so how can I do it? Should I create a variable difference? is important to add that the alert must say if the guess number is bigger or smaller that the random

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM