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 pDecision = "a"
}
else if (randomizer > 67) {
var pDecision = "b"
}
else if (33 < randomizer <= 67) {
var pDecision = "c"
}
document.write(pDecision)
}
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 pDecision = "a"
}
else if (randomizer > 67) {
var pDecision = "b"
}
else if (33 < randomizer <= 67) {
var pDecision = "c"
}
document.write(pDecision)
}
Share
Improve this question
edited Mar 16, 2015 at 1:47
bloodyKnuckles
12.1k4 gold badges31 silver badges38 bronze badges
asked Mar 16, 2015 at 1:44
AidanAidan
511 silver badge5 bronze badges
2
- Obligatory – j08691 Commented Mar 16, 2015 at 1:47
- Also – jkdev Commented Mar 16, 2015 at 1:50
9 Answers
Reset to default 8A bunch of things e 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 pDecision = "a"
}
You would need:
if (randomizer() <= 33) {
var pDecision = "a"
}
3. JavaScript doesn't have three-way parisons
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'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());
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 parisons below to be run against the same number
var randomNumber = Math.random() * 100;
if (randomNumber <= 33) {
var pDecision = "a";
}
else if (randomNumber > 67) {
var pDecision = "b"
}
else {
var pDecision = "c"
}
return pDecision;
}
You can simplify your code to this:
var randomizer = Math.random() * 100;
if (randomizer <= 33) {
var pDecision = "a";
} else if (randomizer > 67) {
var pDecision = "b";
} else if (33 < randomizer && randomizer <= 67) {
var pDecision = "c";
}
alert(pDecision);
randomizer is a variable that is holding a function.
To understand this issue, please try
alert(Math.random());
var value = randomizer(); alert(value);
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());
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.
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;
}
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 paring two numbers, it's paring 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 pDecision = "c"
gets executed.
And that's why you keep getting "c".
EDIT: RobG's ment below explains why 33 < randomizer <= 67
evaluates to true
in JavaScript.