Javascript:定义了一些变量,一些未定义

时间:2015-05-24 04:16:06

标签: javascript html random undefined

我正在写一个基本的赌场javascript游戏,将随机选择3个数字1-10。如果每个号码都是7,它将显示一个警告框,上面写着“你赢了!”#39;在下面的函数中:

function StartSpin(){
        var one;
        var two;
        var three;
        var cone;
        var ctwo;
        var cthree;
        var one = Math.floor((Math.random() * 10) + 1);
        var two = Math.floor((Math.random() * 10) + 1);
        var three = Math.floor((Math.random() * 10) + 1);
        if(one == 1){var cone = "Ace";}
        if(two == 1){var ctwo = "Ace";}
        if(three == 1){var cthree = "Ace";}
        document.getElementsByClassName("Spinner")[0].innerHTML= cone
        document.getElementsByClassName("Spinner")[1].innerHTML= ctwo
        document.getElementsByClassName("Spinner")[2].innerHTML= cthree
    }

在点击按钮开始随机化之前的实际页面上说: - | - | - 。单击它时,它会将 - ' s设置为随机数。每个数字/ - 设置都是未定义的,除非有人会说' Ace'这意味着它是1.所以它可能会说:undefined | Ace | undefined,或undefined | undefined | undefined等。 这是HTML:

<div id="GameOne">
    <h1>~ Game One ~</h1>
    <h2>Try to get three 7's!</h2>
    <span class="so Spinner">--</span> | 
    <span class="st Spinner">--</span> | 
    <span class="sth Spinner">--</span>
    <br/>
    <button id="SpinButton" onclick="StartSpin()">Spin!</button>
</div>

编辑:我重新定义变量以查看是否有助于解决未定义的问题(在javascript代码中为fyi)

5 个答案:

答案 0 :(得分:2)

简短的回答是,如果您随机获取数字undefined,则只提供1以外的变量值。否则它们会保留undefined - 这是JavaScript中变量的默认值。

这是一些严肃清理的逻辑:

http://jsbin.com/milibusaxe/1/edit?html,js,output

function roll() {
  var n = Math.floor((Math.random() * 10) + 1);
  return (n === 1 ? 'Ace!' : n);
}

function StartSpin(){
  var slots = document.getElementsByClassName("Spinner");

  for (var i = 0, e = slots.length; i < e; i++) {
    slots[i].innerHTML = roll();
  }
}

document.getElementById('SpinButton').addEventListener('click', StartSpin);

作为旁注,三个七或七个?可能想在那个决定。

答案 1 :(得分:1)

它们被设置为未定义,因为您只是在随机选择1时设置变量(cone,ctwo,cthree)。我假设如果没有绘制ace,你想要显示数字吗?

function StartSpin() {
    for (var i = 0; i < 3; i++) {
       var num = Math.floor((Math.random() * 10) + 1);

       if (num == 1) {
           num = 'Ace';
       }

       document.getElementsByClassName("Spinner")[i].innerHTML = num;
    }
}

答案 2 :(得分:1)

仅当- (void)appInviteDialog: (FBSDKAppInviteDialog *)appInviteDialog didCompleteWithResults: (NSDictionary *)results { NSLog(@"%@", results); } - (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didFailWithError:(NSError *)error { NSLog(@"%@", error); } conectwo(分别)等于1时才定义ctreeonetwo。否则,变量未启动,这就是未定义的原因。

请参阅undefined

你可以试试这个: https://jsfiddle.net/0jaxL1hb/1/

答案 3 :(得分:0)

看起来你在变量如何工作方面遇到了一些麻烦。 conectwo,&amp;在大多数情况下,cthree未定义,除非您获得1。此外,您只需在创建变量时在变量前面声明var。以后的引用只使用变量名:

var i = 1;
var j = i + 5;
console.log("i is", i, "and j is", j); // will print `i is 1 and j is 5`

没有设定值的声明变量将为undefined

var k;
console.log(k); // will print `undefined`

在您的代码中,您尝试将1转换为字符串“Ace”,但最终会丢弃onetwothree中的值在所有其他情况下。这应该工作:

function StartSpin() {
  // Function to make a number between 1 and 10, if 1 return "Ace" instead
  function randomNumberOrAce() {
    var number = Math.floor((Math.random() * 10) + 1);

    // Check here if it's a `1`, and return "Ace instead", otherwise return the previously stored value
    if (number === 1) {
      return "Ace";
    } else {
      return number;
    }
  }

  // Fill in the first three elements of ".Spinner" with three random numbers
  document.getElementsByClassName("Spinner")[0].innerHTML = randomNumberOrAce();
  document.getElementsByClassName("Spinner")[1].innerHTML = randomNumberOrAce();
  document.getElementsByClassName("Spinner")[2].innerHTML = randomNumberOrAce();
}
<div id="GameOne">
  <h1>~ Game One ~</h1>
  <h2>Try to get three 7's!</h2>
  <span class="so Spinner">--</span> |
  <span class="st Spinner">--</span> |
  <span class="sth Spinner">--</span>
  <br/>
  <button id="SpinButton" onclick="StartSpin()">Spin!</button>
</div>

答案 4 :(得分:-1)

您已在不同范围内将变量声明两次。

LINKS( WAREHOUSES, VENDORS): COST, VOLUME;
相关问题