FAQ: Code Challenges: JavaScript Fundamentals - tipCalculator()


#1

This community-built FAQ covers the “tipCalculator()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise tipCalculator()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Hi
Question regarding syntax

What is the difference here between 1. (=) and 2. (===)?
The outcome of 1. is 5, and outcome of 2 is 20 (which is correct).

  1. // As a function declaration:
    function tipCalculator(quality, total) {
    if(quality===‘bad’) {
    return (total * 0.05);
    } else if(quality===‘ok’) {
    return (total * 0.15);
    }else if (quality===‘good’) {
    return (total* .20)
    } else if (quality===‘excellent’) {
    return (total0.30);
    } else { (total
    0.18);
    }
    };

console.log(tipCalculator(‘good’, 100))

    1. // As a function declaration:
      function tipCalculator(quality, total) {
      if(quality=‘bad’) {
      return (total * 0.05);
      } else if(quality=‘ok’) {
      return (total * 0.15);
      }else if (quality=‘good’) {
      return (total* .20)
      } else if (quality=‘excellent’) {
      return (total0.30);
      } else { (total
      0.18);
      }
      };

console.log(tipCalculator(‘good’, 100))


FAQ: Code Challenges: JavaScript Fundamentals - calculateWeight()
#3

= means assign:

var x = "assign string to variable"

while === is identity equality.


#4

Hi Team.

I used a case statement and the output is printing with no errors. Yet i am not being marked as correct. Any idea why?

const tipCalculator = (quality, totalBill) => {
var tip;
switch (quality) {
case ‘bad’:
tip = Math.round(totalBill * 0.05); // 5% tip
return tip ;
break;
case ‘ok’:
tip = Math.round(totalBill * 0.15); // 15% tip
return tip;
break;
case ‘good’:
tip = Math.round(totalBill * 0.2); // 20% tip
return tip;
break;
case ‘excellent’:
tip = Math.round(totalBill*0.3); // 30% tip
return tip;
break

default: 
  tip = Math.round(totalBill*0.18); // 18% tip
  return tip;
  break;

}
}
;


#5

figured it ot - don’t be tidy and round the number. in practice its easier than having cents to add in!