[英]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.