Fun with random numbers - just for the heck of it

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

Link to the challenge:

This doesn’t do what you expect. => is not >=

What do you mean by this? chance < 0.5 should work fine.

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 found three errors in your code :

  1. I don’t know how your code is giving you output. You define your chance variable after the function which should give you the reference error.
  2. The operator is either greater than equal to i.e. >= or less than equal to i.e. <=. Your operator is not correct.
  3. @kzgoldenwing is totally correct in explaining your actual problem.

Thanks everyone for the help. Sorry for the late reply - had to handle other obligations and a ton of driving over the weekend.

kzgoldenwing - bingo. I added chance to the console.log(trueOrFalse()); -----so the new line is: console.log(trueOrFalse(chance));

Works correctly now :slight_smile:


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

console.log(chance);

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you. Sorry about that.