Just for kicks: Trying to create a function that returns a message based on a random number between 0 and 1. I think its close but I don’t understand why it does not work correctly. It always does one output or the other regardless of what the Math.random() spits out. Any pointers on what I’m missing much appreciated.
**Your code so far**
function trueOrFalse(chance) {
if (chance => .5){
return "'I'm Learning!'";
}
return "'I'm going to learn something....eventually...'";
}
trueOrFalse(chance);
let chance = Math.random()
console.log(trueOrFalse());
console.log(chance);
function trueOrFalse(chance) {
if (chance <= .5){
return "'I'm Learning!'";
}
return "'I'm going to learn something....eventually...'";
}
trueOrFalse(chance);
let chance = Math.random();
console.log(trueOrFalse());
console.log(chance);
Output:
‘I’m Learning!’
0.28574277299776274
Output is always ‘I’m Learning!’ - regardless of a number over or under .5. if I change the operator to > or < it will change the output, but it is then locked and again not dependent on the random number. How do I make the output dependent on the random number?
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0
Challenge: Use Conditional Logic with If Statements
I think the problem is that you forgot to pass a the parameter ‘chance’ to the function:
console.log(trueOrFalse());
If the argument is missed, the local variable change would be undefined. When you compared undefined chance to 0.5, the function will always render “I’m Learning!”.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.