-1

I'm still pretty new to coding and javascript, but I wrote the code below as a three way randomizer. Everything seems to be working fine, except that no matter how many times I run the code the return is whatever is plugged in as "c". I was wondering if anyone can give me some quick advice on what to do to fix this. Thanks.

var random = function() {
  var randomizer = function() {
    Math.random() * 100
  }

  if (randomizer <= 33) {
    var compDecision = "a"
  }
  else if (randomizer > 67) {
    var compDecision = "b"
  }
  else if (33 < randomizer <= 67) {
    var compDecision = "c"
  }

  document.write(compDecision)
}
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
Aidan
  • 51
  • 1
  • 5

9 Answers9

8

A bunch of things come to mind right away:

1. JavaScript doesn't have implicit returns

So this doesn't do what you think:

var randomizer = function() {
  Math.random() * 100
}

That function returns undefined. You need:

var randomizer = function() {
  return Math.random() * 100
}

2. Parentheses are not optional in JavaScript function calls

So this also doesn't do what you think:

if (randomizer <= 33) {
    var compDecision = "a"
}

You would need:

if (randomizer() <= 33) {
    var compDecision = "a"
}

3. JavaScript doesn't have three-way comparisons

So this doesn't do what you think:

else if (33 < randomizer <= 67)

You would need:

else if (33 < randomizer() && randomizer() <= 67)

Lastly, as others have mentioned, defining randomizer as a function actually doesn't make sense in the first place. For your random function to do what you want (produce 'a', 'b', or 'c' with roughly equal probability), you really want to produce a single random value at the start of the function and reuse it:

function random() {
  var randomizer = Math.random() * 100;

  if (randomizer <= 33) {
    return 'a';
  } else if (randomizer <= 67) {
    return 'b';
  } else {
    return 'c';
  }
}

console.log(random());

Hopefully that helps.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1

You wasn't correctly calling the randomizer function. I've removed it as it wasn't really necessary in this case:

var random = function() {
    // I assume you want the comparisons below to be run against the same number
    var randomNumber = Math.random() * 100;

    if (randomNumber <= 33) {
        var compDecision = "a";
    }
    else if (randomNumber > 67) {
        var compDecision = "b"
    }
    else {
        var compDecision = "c"
    }

    return compDecision;
}
user2994359
  • 400
  • 4
  • 13
1

You can simplify your code to this:

var randomizer = Math.random() * 100;

if (randomizer <= 33) {
  var compDecision = "a";
} else if (randomizer > 67) {
  var compDecision = "b";
} else if (33 < randomizer && randomizer <= 67) {
  var compDecision = "c";
}

alert(compDecision);
jkdev
  • 11,360
  • 15
  • 54
  • 77
sideroxylon
  • 4,338
  • 1
  • 22
  • 40
  • Yes, you're right, jquery is not required. I'd started down a different way and forgot to remove it. – sideroxylon Mar 16 '15 at 01:56
  • It can be a lot simpler than that, the last comparison isn't required since if it isn't one of the first two, it must be the last. ;-) – RobG Mar 16 '15 at 01:58
1

Dan Tao's answer is great, in addition there doesn't seem to be any point to a one–line function that is only called once, so:

var n = Math.random() * 100;

However, the document.write part probably should be separate as you likely want to call this function in ways where always writing the result to the current document isn't appropriate.

Lastly, you only need test two of the three conditions since if it isn't either of the first two, it must be the third. You use the conditional operator for that:

function random() {
  var n = Math.random() * 100;
  return n <= 33? 'a' : n <= 67? 'c' : 'b';
}

document.write(random()); 
RobG
  • 142,382
  • 31
  • 172
  • 209
0

randomizer is a variable that is holding a function.

To understand this issue, please try

alert(Math.random());

var value = randomizer(); alert(value);

Tai Ly
  • 311
  • 1
  • 7
  • This is an important point. In JavaScript a variable can hold any object, and functions in JavaScript are objects. This isn't true in every language so it sometimes trips people up. – jkdev Mar 16 '15 at 02:17
  • Just one correction -- When you put parentheses () after the function name, it "invokes" the function, which then returns a value. For example, the statement `var x = Math.random()` assigns a number to x, and `var x = Math.random` assigns a function to x. Try it yourself on [JS Bin](http://jsbin.com/) to get a feel for how it works. – jkdev Mar 16 '15 at 02:38
0

I think, you can do the same thing way like this:

function Randomize(){
    rng = Math.random() * 3;
    rng = Math.round(rng);
    switch(rng) {
    case 0:
        return "a"
        break;
    case 3:
        return "a"
        break;
    case 1:
        return "b"
        break;
    case 2:
        return "c"
        break;
    };
};
alert(Randomize());
Rundom
  • 87
  • 1
  • 7
0

JavaScript requires a semicolon after each statement. However, if/then/else shouldn't be followed by a semicolon.

if (a < 10) {
  alert("Less than ten.");
} else {
  alert("Ten or more.");
}

Sometimes semicolons are automatically inserted where they belong, but you shouldn't rely on this -- it's best to type them yourself.

jkdev
  • 11,360
  • 15
  • 54
  • 77
  • You definitely should use [JSLint](http://jslint.com/) or similar tools to check your code for missing semicolons, unclosed brackets, and many other issues. Sometimes it's the little things that matter most! – jkdev Mar 16 '15 at 02:13
  • This is simply not true. The semicolons in JS are required only in some places. https://stackoverflow.com/a/4002248 – karoluS Aug 12 '20 at 08:28
  • It depends what you mean by "required." I guess technically, semicolons are required to be either (1) typed manually or (2) inserted automatically (ASI). – jkdev Aug 13 '20 at 04:18
0

The keyword var is used to create/declare/initialize a variable. This is done only once per variable.

Also it's good practice to declare a variable at the top of the function that contains it.

function() {
  var result;

  if (condition1 === true) {
    result = "a";
  } else if (condition2 === true) {
    result = "b"
  } else {
    result = "c"
  }

  return result;
}
jkdev
  • 11,360
  • 15
  • 54
  • 77
0

In your code, "randomizer" is a function. To execute it and get the value it returns, you'd have to put a pair of parentheses after it: randomizer()

So randomizer <= 33 isn't comparing two numbers, it's comparing a function to a number. Likewise with randomizer > 67. Each of them therefore evaluates to false.

Then, 33 < randomizer <= 67 evaluates to true. Why? I'm not sure. But because it's considered "true", var compDecision = "c" gets executed.

And that's why you keep getting "c".

EDIT: RobG's comment below explains why 33 < randomizer <= 67 evaluates to true in JavaScript.

jkdev
  • 11,360
  • 15
  • 54
  • 77
  • 1
    "*Why? I'm not sure*". The operators have equal precedence so are evaluated left to right. `<` and `<=` coerce their operands to Number. In `33 < randomizer`, *randomizer* is coerced to Number and evaluates to *NaN*, so the expression resolves to *false*. The expression is now `false <= 67`, so *false* is coerced to Number and evaluates to *0*, which is less than 67, so the overall expression evaluates to *true*. – RobG Mar 16 '15 at 04:36