javascriptarraysfunctiontestingmethods

The test keeps coming back as failing for a method. Don't understand why


My Code

// Given variables
const dishData = [
    {
        name: "Italian pasta",
        price: 9.55
    },
    {
        name: "Rice with veggies",
        price: 8.65
    },
    {
        name: "Chicken with potatoes",
        price: 15.55
    },
    {
        name: "Vegetarian Pizza",
        price: 6.45
    },
]
const tax = 1.20;

// Implement getPrices()
function getPrices(taxBoolean) {
    for (const product of dishData) {
        var finalPrice;
        if (taxBoolean == true) {
            finalPrice = product.price * tax;
        }
        else if (taxBoolean == false) {
            finalPrice = product.price;
        }
        else {
            console.log("You need to pass a boolean to the getPrices call!");
            return

        }
        console.log(`Dish: ${product.name} Price $${finalPrice}`);

    }
    
}


// Implement getDiscount()
function getDiscount(taxBoolean, guests) {
    getPrices(taxBoolean);
    if (typeof (guests == 'number') && (guests < 30 && guests > 0)) {
        var discount = 0;
        if (guests < 5) {
            discount = 5;
        
        } else if (guests >= 5){
            discount = 10
        }
        console.log(`Discount is: $` + discount);
        }   
    else; {
        console.log('The second argument must be a number between 0 and 30');
        
    }
}
    

    


// Call getDiscount()
getDiscount(true, 2);
getDiscount(false, 10);

Failing Message I get

FAILED Test Case: getPrices method applies tax when taxBoolean parameter is true Your Code Produced: Dish: Italian pasta Price $11.46\nDish: Rice with veggies Price $10.38\nDish: Chicken with potatoes Price $18.66\nDish: Vegetarian Pizza Price $7.74\nExpected Output Is: Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74

FAILED Test Case: getPrices method does not apply tax when taxBoolean parameter is false Your Code Produced: Dish: Italian pasta Price $9.55\nDish: Rice with veggies Price $8.65\nDish: Chicken with potatoes Price $15.55\nDish: Vegetarian Pizza Price $6.45\nExpected Output Is: Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45

Passed: getPrices method logs expected error message when taxBoolean is not supplied FAILED Test Case: getDiscount method when tax is applied and guests less than 5 Your Code Produced: Dish: Italian pasta Price $11.46\nDish: Rice with veggies Price $10.38\nDish: Chicken with potatoes Price $18.66\nDish: Vegetarian Pizza Price $7.74\nDiscount is: $5\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 Discount is: $5

FAILED Test Case: getDiscount method when tax is applied and guests more than 5 Your Code Produced: Dish: Italian pasta Price $11.46\nDish: Rice with veggies Price $10.38\nDish: Chicken with potatoes Price $18.66\nDish: Vegetarian Pizza Price $7.74\nDiscount is: $10\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 Discount is: $10

FAILED Test Case: getDiscount method when tax is not applied and guests less than 5 Your Code Produced: Dish: Italian pasta Price $9.55\nDish: Rice with veggies Price $8.65\nDish: Chicken with potatoes Price $15.55\nDish: Vegetarian Pizza Price $6.45\nDiscount is: $5\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45 Discount is: $5

FAILED Test Case: getDiscount method when tax is not applied and guests more than 5 Your Code Produced: Dish: Italian pasta Price $9.55\nDish: Rice with veggies Price $8.65\nDish: Chicken with potatoes Price $15.55\nDish: Vegetarian Pizza Price $6.45\nDiscount is: $10\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45 Discount is: $10

FAILED Test Case: getDiscount method logs expected error message when guests count not in range Your Code Produced: Dish: Italian pasta Price $11.46\nDish: Rice with veggies Price $10.38\nDish: Chicken with potatoes Price $18.66\nDish: Vegetarian Pizza Price $7.74\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 The second argument must be a number between 0 and 30

The first few times it didn't pass because some of the variables wherent declared so I fixed that but now I don't understand why I am not passing. Please help. Thank You.

CHALLENGE:

Step 1: In the function getPrices(), give it the parameter of taxBoolean.

Step 2: Inside the getPrices() function, code a for loop that will loop over all the objects inside the dishData array.

Step 3: Inside the for loop, declare a finalPrice variable, without assigning it a value.

Step 4: Still inside the for loop, add an if condition, checking that the taxBoolean is set to true. Inside the if block, multiply the following: * the price of the currently looped-over object from the dishData array, and * the tax value. Assign the multiplied value to the finalPrice variable.

Step 5: Right after the if condition, add an else if, checking if the value of taxBoolean is false. Inside this condition's block, assign the currently looped-over dish price property in the dishData array to the finalPrice variable.

Step 6: Code the else case, and inside of it, add two lines of code:

A console log of the string:

"You need to pass a boolean to the getPrices call!"

return (to "jump out" of the further function execution)

Step 7: After all the conditional's statements, but still inside the for loop, code another console log with four arguments:

The string "Dish: "

The value of currently looped-over dish object's name property

The string "Price: $"

The value of the finalPrice variable

Step 8: You're finshed with the getPrices() function, and now you're ready to code another function. Give the getDiscount() function, two parameters: the taxBoolean and the guests parameter.

Step 9: Inside the getDiscount() function, on the very first line of its body, invoke the getPrices() function, passing it the taxBoolean as an argument.

Step 10: On another line, you need to implement your defensive coding skills, and check that the type of the guests parameter is 'number' and that the value of the guests variable is greater than zero and less than 30. If all these conditions return true, code the body of the conditional as described in the next step. If they don't all return true, code the body of the else conditional as instructed in step 12.

Step 11: Inside the if statment, declare a new variable, named discount, and set it to 0. On the next line, add another if...else if: in the first if, you'll check that the value of the guests variable is less than 5. If that's the case, reassign the value of the discount variable to 5;

Inside the else if condition, check that the value of the guests variable is greater than or equal to 5 - if that's the case, reassign the discount variable to 10. Console log the following after closing your else-if statement: 'Discount is: $' + discount); Step 12: In the else condition, console log the following string: 'The second argument must be a number between 0 and 30'. Since you've finished declaring both the getPrices() and the getDiscount() functions, you can now invoke the getDiscount() function several times, with various combinations of arguments, to check the behavior.


Solution

  • Now, if you REALLY want this to pass according to the criteria, you will need to concatenate strings. You should not be logging at all. Your functions should be returning strings.

    const dishData = [
      { name: "Italian pasta"         , price:  9.55 },
      { name: "Rice with veggies"     , price:  8.65 }, 
      { name: "Chicken with potatoes" , price: 15.55 },
      { name: "Vegetarian Pizza"      , price:  6.45 }
    ];
    const tax = 1.20; // +20%
    
    function getPrices(taxBoolean) {
      let results = [];
      for (const product of dishData) {
        let finalPrice;
        if (taxBoolean) {
          finalPrice = product.price * tax;
        } else {
          finalPrice = product.price;
        }
        results.push(`Dish: ${product.name} Price: $${finalPrice}`);
      }
      return results.join(' ');
    }
    
    function getDiscount(taxBoolean, guests) {
      let result = getPrices(taxBoolean);
      if (typeof guests === 'number' && (guests > 0 && guests < 30)) {
        let discount = 0;
        if (guests < 5) {
          discount = 5;
        } else if (guests >= 5) {
          discount = 10
        }
        return result + ' Discount is: $' + discount;
      } else; {
        return result + ' The second argument must be a number between 0 and 30';
      }
    }
    
    // Test Case: getPrices method applies tax when taxBoolean parameter is true
    const expected1 = 'Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74';
    
    // Test Case: getPrices method does not apply tax when taxBoolean parameter is false
    const expected2 = 'Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45';
    
    // Test Case: getDiscount method when tax is applied and guests less than 5
    const expected3 = 'Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 Discount is: $5';
    
    // Test Case: getDiscount method when tax is applied and guests more than 5
    const expected4 = 'Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 Discount is: $10';
    
    // Test Case: getDiscount method when tax is not applied and guests less than 5
    const expected5 = 'Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45 Discount is: $5';
    
    // Test Case: getDiscount method when tax is not applied and guests more than 5
    const expected6 = 'Dish: Italian pasta Price: $9.55 Dish: Rice with veggies Price: $8.65 Dish: Chicken with potatoes Price: $15.55 Dish: Vegetarian Pizza Price: $6.45 Discount is: $10';
    
    // Test Case: getDiscount method logs expected error message when guests count not in range
    const expected7 = 'Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 The second argument must be a number between 0 and 30';
    
    // Validate tet cases...
    console.log([
      getPrices(true) === expected1,
      getPrices(false) === expected2,
      getDiscount(true, 4) === expected3,
      getDiscount(true, 5) === expected4,
      getDiscount(false, 4) === expected5,
      getDiscount(false, 5) === expected6,
      getDiscount(true, 30) === expected7,
    ].every(result => result === true));
    .as-console-wrapper { top: 0; max-height: 100% !important; }


    Original response

    You can greatly simplify your logic, if you exit out of (or throw an error in) your function if your meet an illegal condition.

    Also, per:

    Step 10: On another line, you need to implement your defensive coding skills, and check that the type of the guests parameter is 'number' and that the value of the guests variable is greater than zero and less than 30.

    This means that you can only serve groups of 1 to 29 people.

    Edit

    Per this scenario:

    FAILED Test Case: getDiscount method logs expected error message when guests count not in range Your Code Produced: Dish: Italian pasta Price $11.46\nDish: Rice with veggies Price $10.38\nDish: Chicken with potatoes Price $18.66\nDish: Vegetarian Pizza Price $7.74\nThe second argument must be a number between 0 and 30\nExpected Output Is: Dish: Italian pasta Price: $11.46 Dish: Rice with veggies Price: $10.38 Dish: Chicken with potatoes Price: $18.66 Dish: Vegetarian Pizza Price: $7.74 The second argument must be a number between 0 and 30

    I think the expected output should be simply:

    The second argument must be a number between 1 and 29

    Because, if the guest count is out of range, you should display the error and seize printing of the dishes. Telling the user that there was an error after processing is a waste of time.

    const dishData = [
      { name: "Italian pasta"         , price:  9.55 },
      { name: "Rice with veggies"     , price:  8.65 }, 
      { name: "Chicken with potatoes" , price: 15.55 },
      { name: "Vegetarian Pizza"      , price:  6.45 }
    ];
    const tax = 1.20; // +20%
    
    const isBoolean = (v) => (typeof v === 'boolean') || (v instanceof Boolean);
    
    const getPrices = (applyTax = true) => {
      if (!isBoolean(applyTax)) {
        console.log('You need to pass a boolean to the getPrices call!');
        return; // Exit...
      }
      for (const { name, price } of dishData) {
        const finalPrice = applyTax ? price * tax : price;
        console.log(`Dish: ${name} Price $${finalPrice}`);
      }
    }
    
    const getDiscount = (guests = 1, applyTax = true) => {
      if (guests < 1 || guests > 29) {
        console.log('The number of guests must be between 1 to 29');
        return; // Exit...
      }
      getPrices(applyTax);
      const discount = guests < 5 ? 5 : 10;
      console.log(`Discount is: $` + discount);
    }
    
    getDiscount(2);
    getDiscount(10, false);
    .as-console-wrapper { top: 0; max-height: 100% !important; }