-2

I am pretty new to Javascript, and it seems like i didnt understand the if else statements correctly.

I have a script which will make the visitor go to 1 of 4 websites, but the 2 last sites in my code does not work.

<script>

setTimeout(function() { 

var r = Math.random();
if(r > 0.49) {
window.location.replace("1.html");
}

else if(r < 0.48) {
window.location.replace("2.html");
}

if (r == 0.48){
    window.location.replace("maybe.html");
}

else if (r == 0.49){
    window.location.replace("4.html");
}

}, 1);
</script>

Is how my code looks like right now. How would it need to look to make it work?

Fred Lu
  • 21
  • 4
  • 2
    what's the point of setting a timeout of 1 ms ? – Karan Feb 20 '15 at 17:29
  • Please define "does not work". – Teemu Feb 20 '15 at 17:30
  • 2
    Code seems to be okay but what is possibility of math.random returning 0.48 and 0.49 exactly? it is very rare. – Jenish Rabadiya Feb 20 '15 at 17:30
  • @karan http://stackoverflow.com/questions/12051769/why-are-some-javascript-developers-using-settimeout-for-one-millisecond#answer-12051906 – blex Feb 20 '15 at 17:31
  • @Karan i set the timeout to 1ms because i wanted to test. Normally there is a loading page on 1.7 seconds before it redirects - however in this case, i wanted it to load instantly:) – Fred Lu Feb 20 '15 at 18:06
  • @Teemu As you can see i set the ms to 1, however the page does not redirect sometimes, and it just stucks on the loading screen. I assumed this was because my logic was not correct. – Fred Lu Feb 20 '15 at 18:07
  • Please add that to the question itself, it would make the question much more understandable. – Teemu Feb 20 '15 at 18:11

2 Answers2

4

Update

I originally said this looked fine, but I just noticed a problem. There is no branch for r > 0.48 && r < 0.49. Values in this range, such as 0.48342... are more likely than hitting 0.48 or 0.49 exactly, and these are completely unaccounted for, which I assume was not your intention. A simple else branch is always a good idea, or you should account for these cases explicitly.

Original

Your logic looks fine to me. Reduce your problem:

function randomizeText() {
  var r = Math.random();
  var result = '';
  if (r > 0.49) {
    result = 'A';
  }
  else if (r < 0.48) {
    result = 'B';
  }
  else if (r == 0.48){
    result = 'C';
  }
  else if (r == 0.49){
    result = 'D';
  }
  document.getElementById('output').innerText = result;
  document.getElementById('random-number').innerText = 'Number was: ' + r;
}

randomizeText();
<button type="button" onclick="randomizeText();">Randomize!</button><br>
<div id="output"></div>
<div id="random-number"></div>

Note that it's going to be very very unlikely that you'll hit either of the last 2 conditions.

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
3

You can replace your entire code block with these 2 lines, and they will do what you want:

var r = Math.floor(Math.random() * 4) + 1;
window.location.replace(r+".html");

Explanation:

Your code is actually working. The problem is that the number returned by Math.random() is a random number between 0 and 1 (it might be 0.5544718541204929 ), and will almost NEVER be exactly 0.48 or 0.49, but will almost always be between those two numbers.

A better solution would be:

var r = Math.floor(Math.random() * 4) + 1;

and then test if number is 1, 2, 3 or 4.

Example:

jsFiddle Demo //jsFiddle temporarily not saving fiddles

var r = Math.floor(Math.random() * 4) + 1;

if(r ==1) {
    alert("1.html");
}else if(r==2){
    alert("2.html");
}else if(r==3){
    alert("3.html");
}else{
    alert("4.html");
}

BUT there is no need for the entire IF block. Just do this:

var r = Math.floor(Math.random() * 4) + 1;
window.location.replace(r+".html");
//alert( r + ".html" );

In response to the this question, submitted as a comment: I want it to be page 1 and page 2 has is almost 50/50, and the last 2 is pretty rare

This would give odds of 1% for cases 3 and 4.

var r = Math.floor(Math.random() * 100) + 1; //return number between 1 and 100

if(r <=48) {
    alert("1.html");
}else if(r<=98){
    alert("2.html");
}else if(r==99){
    alert("3.html");
}else{ //r==100
    alert("4.html");
}

If you desire slightly larger odds:

if(r <=40) { //40% chance
    alert("1.html");
}else if(r<=80){ //40% chance
    alert("2.html");
}else if(r<=90){ //10% chance
    alert("3.html");
}else{ //r is between 91 and 100, 10% chance
    alert("4.html");
}
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • I understand this pretty well. However i want it to be page 1 and page 2 has is almost 50/50, and the last 2 is pretty rare.. So could i not * with 100 and do it the same way as i already did? Just with 48 as an example, instead of 0.48 ? @gibberish – Fred Lu Feb 20 '15 at 17:57
  • I accidentally submitted my comment before finishing it. I edited it now :) – Fred Lu Feb 20 '15 at 18:03
  • @FrederikLund See my modified answer – cssyphus Feb 20 '15 at 18:22