简体   繁体   English

为什么Math.random在我的代码上不起作用?

[英]Why Math.random doesn't work at my code?

This is my lift program. 这是我的电梯程序。 But Math random doesn't work. 但是随机数学是行不通的。

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!");

Two problems 两个问题

First, change your lift.position function so that it returns the value (rather than assign it to an inaccessible local variable). 首先,更改您的lift.position函数,使其返回值(而不是将其分配给不可访问的局部变量)。

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

Second, make sure you call the lift.position() function (use parentheses to call a function)... 其次,请确保您调用lift.position()函数(使用括号来调用函数)...

lift.position()

 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!"); 

From what I could understand, this is the solution 据我了解,这是解决方案

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

      } 

//corrected the call to lift.position as lift.position() //将对lift.position的调用更正为lift.position()

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

You have assigned a function to position property of the person object. 您已为人员对象的位置属性分配了一个功能。 To invoke the method you need to call function, ie lift.position() not lift.position. 要调用该方法,您需要调用函数,即lift.position()而不是lift.position。 It will be better if you change the name of the property from "position" to "getPosition". 如果将属性的名称从“ position”更改为“ getPosition”,那就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM