[英]Problem creating Repl.it function boolean for javascript
一直在寻找答案,以了解我对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)
出现错误。 有社区解决方案吗?
欢迎使用Javascript。 但是我认为您需要通过w3school js教程重新开始学习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)
代码的错误只是您使用等号=而不是逻辑运算符==(等于)
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 ),那么您无需将其与“真实的”进行比较。
但是,从严格的平等意义上讲,比较将确认其值是真实的,而不是另一个真实值(例如字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.