If -else - exercise

Hi coders,
I was trying this exercise but I don’t understand why give this error :thinking:

addWithSurcharge(1, 1) does not return 4 , but 3 .

This is the exercise :

Write a function addWithSurcharge that adds two amounts with surcharge. For each amount less than or equal to 10 , the surcharge is 1 . For each amount greater than 10 , the surcharge is 2 . The call addWithSurcharge(5, 15) should return 23 .

And this is my script:


var addWithSurcharge = function(amount1 , amount2){

var surcharge = amount1 + amount2;


if (( amount1 <= 10 || amount1 > 10) && (amount2 <=10 || amount2 > 10)) {

return surcharge + 1 ;

} else {

return surcharge + 2;

}

};

Thanks for help !!!
CamCode

In this line you are first checking if amount is less than/equal to 10 or greater than 10. That will always evaluate to true since every option is true. So now we have essentially:
if ((true) && (amount2 <= 10 || amount2 > 10))
Same thing for amount2. It will always be either less than//equal to 10 or greater than 10. So now it’s:
if ((true) && (true))
Of course that’s always true. So return surcharge + 1; will always follow.

Try this (I didn’t run this code):

var addWithSurcharge = function(amount1 , amount2){ 
return calcSurcharge(amount1) + calcSurcharge(amount2);
};

function calcSurcharge(amount){
if (amount <=10){
return amount + 1;
}
else (amount > 10) {
return amount + 2;
}
}
1 Like

Doesn’t work because I have to follow the exercise script.
I tried to break the condtions but not much has changed.

var addWithSurcharge = function(amount1 , amount2){

var surcharge = amount1 + amount2;


if (( amount1 <= 10 || amount2 <= 10)  {

return surcharge + 1 ;

}; 

return surcharge + 2;

This doesn’t work either, maybe I was wrong with the Boolean logic and I’m still stuck

@camcode The function needs to return a total which includes the two amounts passed to the function as well as the surcharge for each amount.

function addWithSurcharge(amount1, amount2) {
  function calcSurcharge(amount) {
    if (amount <= 10) {
      return 1;
    } else {
      return 2;
    }
  }
  
  var totalAmounts = amount1 + amount2;
  return totalAmounts + calcSurcharge(amount1) + calcSurcharge(amount2);
}

EDIT: I update my solution to reflect the use of if/else instead of a ternary operator, since I assume the point of the challenge was to use an if/else statement.

1 Like

I’d read the prompt again, carefully.

For each amount less than or equal to 10 , the surcharge is 1 . For each amount greater than 10 , the surcharge is 2 . The call addWithSurcharge(5, 15) should return 23 .

How do you get 23 from 5 and 15? What is the surcharge value vs the sum of the two numbers? I think that this question should help you see the error in your logic.

In general, you want to keep you logic clean and simple. Get something that works correctly and is simple first. If you want, later you can go back and try to reduce the number of lines. But simplicity and readability improves maintainability and saves you time in the future.

A function that adds two amounts with surcharge. For each amount less than or equal to 10 , the surcharge is 1 . For each amount greater than 10 , the surcharge is 2 . The call addWithSurcharge(5, 15) should return 23 .

Using the example for a result of 23:
=> 5 is less than or equal to 10, so you have to add this amount with surcharge of 1 => 5 + 1 (a)
=> 15 is greater than 10, so you have to add this amount with surcharge of 2 = > 15 + 2 (b)

The result is a + b (6 + 17) = 23

This is my interpretation.

1 Like

This is not valid else syntax.

1 Like

Sorry my bad. I’ve updated my suggestion.

1 Like

Thank you all for the suggestions, it’s my fault that I can’t think and I find it hard to break problems

Keep going. This is the place for us to discuss the challenges and the best ways to solve them.

1 Like

I’m trying to try to consolidate the basics, but I’ve been struggling for almost two years without having great results. I do little things, but sometimes they seem difficult.

Coding is hard. I know it feels uncomfortable to struggle with hard things, but it is normal for code to be hard.

1 Like

you need to check each individuale value, as each individual value will need the surcharge separately

otherwise how it could become 23 if the starting values are 5 and 15? the 5 have a surcharge of 1, the 15 of 2, so you have 5+1+15+2=23

try now in solving the algorithm

1 Like

Try this code. This is giving the correct answer.

var addWithSurcharge = function(amount1 , amount2){

var surcharge = amount1 + amount2;

if (amount1 <= 10)
{
 surcharge = surcharge + 1;
}
else{
 surcharge = surcharge + 2;
}

if (amount2 <= 10)
{
 surcharge = surcharge + 1;
}
else{
 surcharge = surcharge + 2;
}
 document.body.innerHTML = surcharge;
}
addWithSurcharge(5, 15);
``````

@GirishS, I don’t think that just handing someone the code that works helps them learn how to solve similar problems in the future.

let addWitheSurcharge = function (n_1 , n_2) {

    let calc = n_1 + n_2

    if (n_1 <=10 && n_2<=10) {

        let surcharge=calc+ 2

        console.log(surcharge)

    } else if (n_1>10 && n_2>10) {

        let surcharge= calc +4

        console.log(surcharge)

    }else if(n_1<=10 && n_2 >10) {

        let surcharge=calc+3

        console.log(surcharge)

    }else {

        let surcharge= calc+3

        console.log(surcharge)

    }

}

let arggs= addWitheSurcharge(5 , 15)

I hope this work if you still didnt figured it out …