【问题标题】:Asserting properties are present producing unexpected results断言属性存在会产生意想不到的结果
【发布时间】:2023-01-18 02:40:47
【问题描述】:

所以这是我的代码

let user = {
    fName: 'Kado',
    lName: 'Kliewer',
    uName: 'kKliewer23',
    age: 18,
};

function helloUser(user) {
    if(user.fName === true && user.lName === true && user.age >= 18) {
        console.log(`Hello ${user.uName}. Welcome to the world of wonders.`);
    } else {
        console.log("Please get a parent's permission before playing this game.")
    }
};

helloUser(user);

我是编码和 JavaScript 的新手,但我对对象和函数之间的关系有点困惑。当我运行我的代码时,它会记录“请在玩这个游戏之前获得父母的许可。

我已经尝试使我的 if/else 语句的第一部分等于 true 但它评估为相同的答案。有人可以解释一下并帮助我吗?

【问题讨论】:

  • user.fName === true - 这个条件是false。 (和user.lName === true一样,同理)你是什么条件在那里测试?
  • 您可能是指 if (user.fName && ...),因为 "Kado" 不是 true

标签: javascript node.js


【解决方案1】:

当你使用 user.fName === true && user.lName === true && user.age >= 18

这意味着:user.fName等于trueuser.lName 等于true

由于 user.fName = "Kado" 等,此条件不成功,因此进入 else

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多