【问题标题】:Math functions to log an object based on a random number基于随机数记录对象的数学函数
【发布时间】:2018-05-01 04:38:19
【问题描述】:

如果数学函数是 0、1 或 2,我想获得基于某事物的当前值。有时它只是想让我写它来做。使用 Quokka 我可以看到它记录的内容,但是当它不能正确执行时会发生两个错误之一。它要么记录“无法读取未定义的属性'答案'”,要么即使 random() 的值已更改,它也会记录相同的值。我只是想弄清楚它为什么会这样。

function question(q, answersArr) {
        this.question = q,
        this.answers = answersArr
}

let questionOne = new question("Question One?", ["Answer One", "Answer Two", "Answer Three"]);
let questionTwo = new question("Question Two?", ["Answer Four", "Answer Five", "Answer Six"]);
let questionThree = new question("Question Three?", ["Answer Seven", "Answer Eight", "Answer Nine"]);

let questionArr = [questionOne, questionTwo, questionThree];

let random = function() {
    let randoNum = Math.random() * 3;
    let roundedNum = Math.floor(randoNum);
    return roundedNum
};

console.log(random());

let current;
if (random() === 0) {
    current = array['0']
} else if (random() === 1) {
    current = array["1"]
} else if (random() === 2) {
    current = array['2']
}

console.log(current.answers);
console.log(current);

【问题讨论】:

    标签: javascript undefined console.log


    【解决方案1】:

    每次您拨打random() 时,您都会生成一个 随机数。因此,如果偶然每个随机数与您正在测试的当前数字不匹配,current 将永远不会被分配到。

    只调用一次random(),将其保存在一个变量中,然后检查该变量。

    您还必须引用问题数组的名称,即questionArr,而不是array

    function question(q, answersArr) {
            this.question = q,
            this.answers = answersArr
    }
    
    let questionOne = new question("Question One?", ["Answer One", "Answer Two", "Answer Three"]);
    let questionTwo = new question("Question Two?", ["Answer Four", "Answer Five", "Answer Six"]);
    let questionThree = new question("Question Three?", ["Answer Seven", "Answer Eight", "Answer Nine"]);
    
    let questionArr = [questionOne, questionTwo, questionThree];
    
    let random = function() {
        let randoNum = Math.random() * 3;
        let roundedNum = Math.floor(randoNum);
        return roundedNum
    };
    
    const randIndex = random();
    console.log(randIndex);
    
    let current;
    if (randIndex === 0) {
        current = questionArr['0']
    } else if (randIndex === 1) {
        current = questionArr["1"]
    } else if (randIndex === 2) {
        current = questionArr['2']
    }
    
    console.log(current.answers);
    console.log(current);

    此外,如果您愿意,您的代码可以显着缩减:

    function question(q, answersArr) {
            this.question = q,
            this.answers = answersArr
    }
    const questionOne = new question("Question One?", ["Answer One", "Answer Two", "Answer Three"]);
    const questionTwo = new question("Question Two?", ["Answer Four", "Answer Five", "Answer Six"]);
    const questionThree = new question("Question Three?", ["Answer Seven", "Answer Eight", "Answer Nine"]);
    const questionArr = [questionOne, questionTwo, questionThree];
    const random = () => Math.floor(Math.random() * 3);
    const randIndex = random();
    console.log(randIndex);
    const current = questionArr[randIndex];
    console.log(current.answers);
    console.log(current);

    【讨论】:

    • 完美运行谢谢你我现在明白为什么我没有工作了。但是这段代码是一个更大的函数的一部分,我没有发布其余的所以数组的值是 questionArr。
    猜你喜欢
    • 2015-12-20
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2020-07-07
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多