为什么Math.random不能在我的代码中工作?

时间:2015-07-17 04:24:55

标签: javascript function

这是我的电梯计划。但数学随机不起作用。

var person = {
     name: "Gorg",
     position: 2,
     goal: 9
 };
 var lift = {
     position: function() {
         var x = Math.floor((Math.random() * 10) + 1);
     }
 };
 console.log("Ok, buddy! You are at " + person.position + " floor");
 console.log("Lift is at " + lift.position + " floor");

 if (lift.position != person.position) {
     console.log("Please wait a minute, lift's coming");
 } 
 else {
     console.log("Lift is here. Please enter!");
 }

 if (lift.position > person.position) {
     for (i = person.position; i <= lift.position; i++) {
         console.log(i);
     }
 } 
 else if (lift.position < person.position) {
     for (i = person.position; i >= lift.position; --i) {
         console.log(i);
     }
 }  
 else {
     console.log("You're already at this level");
 }

 if (lift.position !== person.position) {
     if (person.goal > person.position) {
         for (i = person.position; i <= person.goal; i++) {
             console.log(i);
         }
     } 
     else if (person.goal < person.position) {
         for (i = person.position; i >= person.goal; --i) {
             console.log(i);
         }
     } 
     else {
         console.log("You're already at this level");
     }
 }
 console.log("Congratulation! You achieve your goal!");

3 个答案:

答案 0 :(得分:1)

两个问题

首先,更改您的lift.position函数,使其返回值(而不是将其分配给无法访问的局部变量)。

position: function() {
  return Math.floor((Math.random() * 10) + 1);
}

其次,请确保调用 lift.position()函数(使用括号调用函数)...

lift.position()

答案 1 :(得分:1)

var person = {
      name: "Gorg",
      position: 2,
      goal: 9
    };
    var lift = {
        position : function() {
        var x = Math.floor((Math.random() * 10) + 1);
    return x;

      }  
    }; 
    console.log("Ok, buddy! You are at " + person.position + " floor");
    console.log("Lift is at " + lift.position() + " floor");

    if (lift.position != person.position) {
    console.log("Please wait a minute, lift's coming");
    }
    else {
    console.log("Lift is here. Please enter!");
    }

    if (lift.position > person.position) {
     for (i = person.position; i <= lift.position; i++) {
        console.log(i);
    }
    } else if (lift.position < person.position ) {
      for (i = person.position; i >= lift.position; --i) {
        console.log(i);
    }
    } else {
      console.log("You're already at this level");
    }

    if (lift.position !== person.position) {
      if (person.goal > person.position) {
       for (i = person.position; i <= person.goal; i++) {
        console.log(i);
    }
    } else if (person.goal < person.position ) {
      for (i = person.position; i >= person.goal; --i) {
        console.log(i);
    }
    } else {
      console.log("You're already at this level");
    }
    }
    console.log("Congratulation! You achieve your goal!");

根据我的理解,这是解决方案

//returned x here;
var lift = {
        position : function() {
        var x = Math.floor((Math.random() * 10) + 1);
    return x;

      } 

//将对lift.position的调用更正为lift.position()

console.log("Lift is at " + lift.position() + " floor");

答案 2 :(得分:0)

您已经分配了一个函数来定位属性 人物。要调用该方法,您需要调用函数,即lift.position()而不是lift.position。如果你从&#34;位置&#34;更改属性的名称会更好。到&#34; getPosition&#34;。