Got into a start up as a business analyst but I need to learn Java for some other tasks in the job :/ I just started using code academy so my knowledge of java is still so limited.
Anyway, I'm trying to produce a random number within a function, but it seems that the number it produces is always produces the same number/result.
var randNum = Math.floor(Math.random()*3)
function getChoice ()
{
if (randNum) = 0) {console.log("Choice 1")}
else if (randNum = 1) {console.log("Choice 2")}
else (console.log("Choice 3")}
}
getComputerChoice()
console.log(randNum)
The code seems to always go to choice 2. When I // choice 2 however, it goes to choice 3. For some reason it can't show choice 1. I have also tried to just use the randNum variable for the last console.log(randNum) line to check if it really randomizes. It does, but only when it is not used together with the function
var randNum = Math.floor(Math.random()*3)
function getChoice ()
{
var choiceRandNum = Math.floor(Math.random()*3)
if (choiceRandNum) = 0) {console.log("Choice 1")}
else if (choiceRandNum = 1) {console.log("Choice 2")}
else (console.log("Choice 3")}
}
getChoice()
console.log(randNum)
For the code above, I tried making a different variable for the function (but the same sa randNum essentially). Now the last line can show different numbers (0,1,2) but the function still doesn't randomize. Any edits that can be made? Please don't use overly complex solutions as I still don't know a lot of java.
Aucun commentaire:
Enregistrer un commentaire