为JavaScript创建Repl.it函数布尔值时出现问题

时间:2018-12-28 00:21:04

标签: javascript function boolean sandbox repl.it

曾经试图寻找答案,以加深我对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)

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

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),则无需将其与“真实的”进行比较。

但是从严格的平等意义上讲,比较将确认它的值是true,而不是另一个真实值(例如字符串)。