简体   繁体   中英

Problem creating Repl.it function boolean for javascript

Been trying to find answers to learn up my amateur understanding of javascript. Working inside Repl.it for my class and as a beginner, so I feel like there's a lot that's been stripped down to extreme basics, which doesn't help when I go looking for a solution.

ORIGINAL PROBLEM IS TO DO THIS:

// 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) {

}

I attempted MANY, MANY different combinations trying to figure out what I was doing incorrectly, and at this point, I just don't recognize what's what anymore. Here's one of my latest guesses:

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

Comes up with an error. Any solutions out there community?

Wellcome to Javascript. But I think you need begin learn js again with w3school js tutorial . It easy to learn.

ORIGINAL PROBLEM IS TO DO THIS:

// 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 latest guesses is is so error:

// 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)

The error with your code is simply your use of the equals sign = instead of the the logical operator == (equal to)

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

If you rewrite your code as below it will run:

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)

In terms of the question and a good answer for it:

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'

note: you don't actually need to use else because the code will only reach

return 'pepperoni pizza' 

if the if expression doesn't find the variable to equal true. A function can only return once. You can think of return as the 'answer' to the function.

you could write

if (vegetarian == true) {

or

if (vegetarian) {

because the if expression will evaluate the contents of the brackets. If vegetarian is 'truthy' ( https://www.w3schools.com/js/js_booleans.asp ) then you don't need to compare it to 'true'.

However in a strict equality sense the comparison will confirm it's value is true as opposed to another truthy value like a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM