How can I set a default return with if/else if/else statements like I do with the default case in a switch statement?
Answer
We can set a default condition when using if/else if/else statements, like we do with the default case in a switch statement, by simply using an else statement after the rest of our if and else if conditions.
Example:
let x = 2;
if (x >= 10){
//do something
} else if (x <= 1){
//do something else
} else {
console.log(`x is ${x}`);
}
The else statement acts as a catch-all for when our if or else if statements do not evaluate to true.
To make your code a bit more readable, you should indent your return statements under the corresponding case. Your switch block should also be indented inside the function.
All of your return statements return (num / total) * total. This simply returns num. What could you change so that it returns the correct tip amount?
const tipCalculator = (quality, cost) => {
let tip = 0;
switch (quality) {
case 'bad' : tip = 0.05; break;
case 'ok' : tip = 0.15; break;
case 'good' : tip = 0.2; break;
case 'excellent' : tip = 0.3; break;
default: tip = 0.18;
}
return Math.round(tip*cost)
};
I am working on a if/if else/ else statement if you are still stuck on this. hold tight
My code so far is as follows
// Write your function here:
const tipCalculator = (quality,total) => {
if (quality === âbadâ) {return total * 0.05}
else if (quality === âokâ) {return total * 0.15}
else if (quality === âgoodâ) {return total * 0.20}
else if (quality === âexcellentâ) {return total * 0.30}
else {return total * 0.18}
}
console.log(tipCalculator(âgoodâ, 100))
// Uncomment the line below when youâre ready to try out your function
// console.log(tipCalculator(âgoodâ, 100)) //should return 20
// We encourage you to add more function calls of your own to test your code!
the difference once I looked at the forums, since mines was similar to yours was the 3 equal signs!
They make the world of difference, otherwise, it will get stuck on the first part of code
In the code you posted, you are not comparing if the quality argument given is equal to âbadâ, âokâ, âgoodâ, etc.; you are assigning that value to the quality parameter.
Remember that the assign operator is â=â. Itâs used to assign a value so, if you write quality = âbadâ, youâre saying to the machine that quality, now, has a value of âbadâ.
To compare, you can use either the equal operator == or the strictly equal operator === (this last one is usually the most recommended as compares both the value and the type).
I would also suggest you use the if / else if / else statement instead of separate if statements. Basically, you write if (first condition) {}, then you start using else if (condition) {} for the rest of the conditions. Finally, you can add an else {} to put a default value like in the exercecise or to cath an error, e.g. you can write else {return âError: please, value the quality of the service with bad, ok, good or excelentâ}. See the example:
const tipCalculator = (quality, total) => {
if (quality === 'bad') {
return total * 0.05;
} else if (quality === 'ok') {
return total * 0.15;
} else if (quality === 'good') {
return total * 0.2;
} else if (quality === 'excelent') {
return total * 0.3;
} else {
return total - 0.18;
}
}
I will paste the code I used for the exercise. I used the swjtch statement:
const tipCalculator = (quality, total) => {
switch (quality) {
case 'bad':
return total * .05;
case 'ok':
return total * .15;
case 'good':
return total * .20;
case 'excellent':
return total * .30;
default:
return total * .18;
}
}
I solved it the same way, but I think your mathematical statement is faulty. It only works if the total is 100. If the total would be 50 for example. You would get (5/50)50 = 0.150 = 5. 5 is 10% of 50 and not 5%.
I have chosen to write an if/else control flow for this exercise, however apparently I have a syntax error somewhere. Can someone please help me with this?!
function tipCalculator(quality, total)
{if quality === âbadâ
{return total * .5}
else if quality === âokâ
{return total * .15}
else if quality === âgoodâ
{return total * .20}
else if quality === âexcellentâ
{return total * .30}
else {return total * .18}
}
console.log(tipCalculator(âgoodâ, 100)