jeudi 30 mai 2019

If statement always returns true [duplicate]

This question already has an answer here:

I am new to javascript and am playing around with random numbers. I made a function that should check a generated number against user input and say if it's the same or not, but it always says "correct" even though in the console it shows that the numbers are different. Below is my code. Can someone see what I am doing wrong?

 <!DOCTYPE html>
 <html>

 <head>
         <meta charset="UTF-8">
         <title>title</title>
 </head>

 <body>
         <input id="inp" value="5">
         <button id="btn">Click me!</button>
         <script>
                 function myfunc() {
                         var comp = Math.floor((Math.random() * 10) + 1);
                         var guess = document.getElementById("inp").value * 1;
                         console.log(comp, guess);
                         if (guess = comp) {
                                 alert("Correct!");
                         } else {
                                 alert("Wrong! The computer's number was " + comp);
                         }
                 }
                 document.getElementById("btn").onclick = myfunc;
         </script>
 </body>

 </html>



Aucun commentaire:

Enregistrer un commentaire