FAQ: Code Challenges: JavaScript Fundamentals - tipCalculator()

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!

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

= means assign:

var x = "assign string to variable"

while === is identity equality.

1 Like

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;

}
}
;

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

Why is this wrong?

const tipCalculator = (quality, total) =>{
switch (quality){
case ‘bad’:
return (total/100)*5;
break;
case ‘ok’:
return (total/100)*15;
break;
case ‘good’:
return (total/100)*20;
break;
case ‘excellent’:
return (total/100)*30;
break;
default:
return (total/100)*18;
break;
}

}

Hi there, what’s wrong with this simpliest piece of code ever possible, however I couldn’t make it, SyntaxError. Checked it millions times.

const calculateWeight = (earthWeight, planet) {
  switch (weight){
          case 'Mercury': planet = earthWeight * 0.378;
          break;
          case 'Venus': planet = earthWeight * 0.907;
          break;
          case 'Mars': planet = earthWeight * 0.377;
          break;
          case 'Jupiter': planet = earthWeight * 2.36;
          break;
          case 'Saturn': planet = earthWeight * 0.916;
          break;
          default: planet = 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
          }
} 

console.log(calculateWeight(100, 'Jupiter'))

Thx.

the syntax error indicates the line which the problem occurs:

const calculateWeight = (earthWeight, planet) {

you try to use what is known as an arrow function:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

yet i don’t see the arrow anywhere

1 Like

Hi, thank you for notice.

Apart from the syntax already mentioned, the variable weight is not defined.

1 Like
const tipCalculator = (quality, total) =>{
	if (quality === 'bad'){
    return total * 0.5
  }
  if (quality === 'ok'){
    return total * 0.15
  }
  if (quality === 'good'){
    return total * 0.20
  }
  if (quality === 'excellent'){
    return total * 0.30
  }else{
    return total * 0.18
  }
}

whats wrong here???

Hello, @danielzivharel523032. Welcome to the forum. There’s a few things we’ll need in order to give you some assistance. A link to the exercise would be very helpful. A description of the specific error or problem you are having will also help. Additionally, if you could post your entire code for the exercise using the </> button as described below that will also help. Thanks!

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

A fifty per-cent tip for bad service? Where do I sign up?

3 Likes

3 posts were split to a new topic: Why is this the case that this error pops up

Is this correct or wrong?
const tipCalculator = (quality, total) => {

switch(quality){
case ‘bad’:
let bad = (5/total)*total;
return bad + ‘%’;
break;
case ‘ok’:
let ok = (15/total)*total;
return ok + ‘%’;
break;
case ‘good’:
let good = (20/total)*total;
return good + ‘%’;
break;
case ‘excellent’:
let excellent = (30/total)*total;
return excellent + ‘%’;
break;
default:
let d = (18/total)*total;
return d + ‘%’;
break;
};

};

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

total cancels out so you are left with 15. Same applies for the other cases, as well.

15 / 100 * total

will be `0.15 * total, which is 15 only when the total is 100.

1 Like

I’m working on the tip calculator project and not sure why my code is not accepted. I have run the code and all of the percentages are showing up correctly. Any advice is much appreciated!

ERROR MESSAGE: If ‘bad’ is entered, the function should return 5% of the total passed in

// Write your function here:

function tipCalculator (quality, total) {
switch(quality) {
case ‘bad’:
return (5/total) * 100 + ‘%’;
break;
case ‘ok’:
return (15/total) * 100 + ‘%’;
break;
case ‘good’:
return (20/total) * 100 + ‘%’;
break;
case ‘excellent’:
return (30/total) * 100 + ‘%’;
break;
default:
return (18/total) *100 + ‘%’;
}
}

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

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/tip-calculator

the exercise just expects a number, not a string representation. so lets you have 5% of 100, the exercise simple expects 5.

Apart from what @stetim94 advised, is your math correct?

5 / 100 * 100  =>  5

5 / 200 * 100  =>  2.5 

5 / 500 * 100  =>  1

Using your math, the tip gets smaller as the bill increases. How would you correct this?