【问题标题】:Javascript if/else statements not workingJavascript if/else 语句不起作用
【发布时间】:2015-02-20 17:26:57
【问题描述】:

我对 Javascript 很陌生,似乎我没有正确理解 if else 语句。

我有一个脚本可以让访问者访问 4 个网站中的 1 个,但我的代码中的最后 2 个网站不起作用。

<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>

我的代码现在的样子。它需要如何工作才能发挥作用?

【问题讨论】:

  • 设置 1 毫秒的超时有什么意义?
  • 请定义“不起作用”。
  • 代码似乎没问题,但是 math.random 返回 0.48 和 0.49 的可能性有多大?这是非常罕见的。
  • @blex 谢谢,不知道。

标签: javascript html


【解决方案1】:

更新

我最初说这看起来不错,但我只是注意到一个问题。 r &gt; 0.48 &amp;&amp; r &lt; 0.49 没有分支。在此范围内的值,例如 0.48342...更有可能,而不是点击 0.480.49完全,而这些完全没有解释,我认为不是你的意图。一个简单的else 分支总是一个好主意,或者你应该明确地考虑这些情况。

原创

我觉得你的逻辑很好。减少您的问题:

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>

请注意,您将非常 非常不太可能达到最后两个条件中的任何一个。

【讨论】:

    【解决方案2】:

    你可以用这两行替换你的整个代码块,它们会做你想做的:

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

    说明:

    您的代码实际上正在运行。问题是 Math.random() 返回的数字是 0 和 1 之间的随机数 (it might be 0.5544718541204929 ),几乎永远不会正好是 0.48 或 0.49,但几乎总是在这两个数字之间。

    更好的解决方案是:

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

    然后测试数字是1、2、3还是4。

    例子:

    jsFiddle Demo //jsFiddle暂时不保存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");
    }
    

    但不需要整个 IF 块。只需这样做:

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

    针对这个问题,作为评论提交:I want it to be page 1 and page 2 has is almost 50/50, and the last 2 is pretty rare

    这将为案例 3 和 4 提供 1% 的几率。

    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(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");
    }
    

    【讨论】:

    • 我非常了解这一点。但是我希望它是第 1 页,第 2 页几乎是 50/50,而最后 2 页非常罕见。所以我不能 * 用 100 并像我已经做的那样做吗?仅以 48 为例,而不是 0.48 ? @胡言乱语
    • 我在完成之前不小心提交了我的评论。我现在编辑了它:)
    • @FrederikLund 查看我修改后的答案
    • 完美解决了我的问题!感谢您的帮助和时间!:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多