 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:

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

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

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

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