Search code examples
javascriptfunctionbooleansandboxreplit

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?


Solution

  • 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.