【问题标题】:Problem creating Repl.it function boolean for javascript为 javascript 创建 Repl.it 布尔函数时出现问题
【发布时间】:2026-02-08 23:55:02
【问题描述】:

一直在尝试寻找答案来了解我对 javascript 的业余理解。在 Repl.it 中为我的班级和初学者工作,所以我觉得有很多东西已经被剥离为极端基础,这在我寻找解决方案时没有帮助。

原来的问题是这样做:

// orderPizza takes in a boolean
// if it is true return the string 'cheese pizza'
// if not, return the string 'pepperoni pizza'
// Example: orderPizza(true) returns 'cheese pizza'

function orderPizza(vegetarian) {

}

我尝试了很多很多不同的组合,试图找出我做错了什么,而在这一点上,我只是不知道什么是什么了。这是我最新的猜测之一:

function (vegetarian) {
let orderPizza = vegetarian;
    if (orderPizza = vegetarian) {
        return ("Cheese Pizza!");
    } else {
        return ("Pepperoni Pizza!");
}
};
let newOrder = vegetarian
console.log(newOrder)

出现错误。社区有什么解决方案吗?

【问题讨论】:

  • 更正:我一开始是这样的:function orderPizza (vegetarian) { let orderPizza =素食; if (orderPizza = 素食) { return ("Cheese Pizza!"); } else { return ("Pepperoni Pizza!"); } };让 newOrder = 素食 console.log(newOrder)

标签: javascript function boolean sandbox repl.it


【解决方案1】:

欢迎使用 Javascript。 但我认为你需要用w3school js tutorial 重新开始学习 js。简单易学。

原来的问题是这样做:

// orderPizza takes in a boolean
// if it is true return the string 'cheese pizza'
// if not, return the string 'pepperoni pizza'
// Example: orderPizza(true) returns 'cheese pizza'

function orderPizza(vegetarian) {
     // check vegetarian is true
     if(vegetarian){
         return 'cheese pizza';
     }else{
         return 'pepperoni pizza';
     }
}

// when you call orderPizza(true). In your function parameter is true
console.log(orderPizza(true));

// when you call orderPizza(true). In your function parameter is false
console.log(orderPizza(false));

你最近的猜测是如此错误:

// your function not have name (function name is name you call function)
// example : function orderPizza(vegetarian). orderPizza is function name. vegetarian is parameter you send to in function 
function (vegetarian) {
    // this is you set orderPizza is vegetarian
    let orderPizza = vegetarian;
    // Comparison operators is '==' or '===' not '='. '=' is Assignment Operators
    if (orderPizza = vegetarian) {
        return ("Cheese Pizza!");
    } else {
        return ("Pepperoni Pizza!");
    }
};
// this is you set orderPizza is vegetarian not call function
// you can call function with name and parameter
// example: let newOrder = orderPizza(true)
let newOrder = vegetarian
console.log(newOrder)

【讨论】:

  • 是的!谢谢,我明白了!确实是我使用错误的比较运算符。这些开始填补我学习曲线的主要空白。超级感谢!
【解决方案2】:

您的代码的错误只是您使用等号 = 而不是逻辑运算符 ==(等于)

https://www.w3schools.com/js/js_comparisons.asp

如果你重写你的代码如下,它将运行:

function (vegetarian) {
    // this is you set orderPizza is vegetarian
    let orderPizza = vegetarian;
    // Comparison operators is '==' or '===' not '='. '=' is Assignment Operators
    if (orderPizza == vegetarian) {
        return ("Cheese Pizza!");
    } else {
        return ("Pepperoni Pizza!");
    }
};
// this is you set orderPizza is vegetarian not call function
// you can call function with name and parameter
// example: let newOrder = orderPizza(true)
let newOrder = vegetarian
console.log(newOrder)

就问题和一个好的答案而言:

function orderPizza (vegetarian){
    if (vegetarian == true){
        return 'cheese pizza'
    }
    return 'pepperoni pizza'
}

order1 = orderPizza(true)
order2 = orderPizza(false)

console.log(order1)
// will log 'cheese pizza'
console.log(order2)
// will log 'pepperoni pizza'

注意:你实际上不需要使用else,因为代码只会到达

return 'pepperoni pizza' 

如果 if 表达式没有找到等于 true 的变量。一个函数只能返回一次。您可以将 return 视为函数的“答案”。

你可以写

if (vegetarian == true) {

if (vegetarian) {

因为 if 表达式将计算括号的内容。如果素食主义者是“真实的”(https://www.w3schools.com/js/js_booleans.asp),那么您无需将其与“真实”进行比较。

但是在严格相等的意义上,比较将确认它的值是真实的,而不是另一个真实的值,如字符串。

【讨论】:

  • 非常感谢您:此回复非常有用且内容丰富。您通过举例说明使函数运行所需的时间很少,从而帮助我成为更简洁的编码器。比较算子现在正式钻进我的大脑了,哈哈。非常感谢,@Happy Machine!
  • 如有任何未来问题,请随时与我联系。很高兴为您提供帮助!