Wednesday, May 19, 2021

Javascript Practice 49: JS Hero - else if

 
Question: 

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 and less than or equal to 20, the surcharge is 2. For each amount greater than 20, the surcharge is 3.

Example: addWithSurcharge(10, 30) should return 44.




Answer: 

function addWithSurcharge(a, b) {

    let sum = a + b;

    if (a <= 10) {
            sum += 1;
} else if (a <= 20) {
            sum += 2;
} else     {sum += 3;}

    if (b <= 10) {
            sum += 1;
} else if (b <= 20) {
            sum += 2;
} else     {sum += 3;}

    return sum;
}; 





or 




function lineItemToSurcharge(a) {
  
if ( a <= 10) {
            
            return 1;
  
} else if (a <= 20) {

            return 2

  } else {

            return 3
}}

function addWithSurcharge (a,b) { 
  
let sum = a + b; 
return sum + lineItemToSurcharge(a) + lineItemToSurcharge(b); 
}





> the first solution is fairly easy to understand
>> we declare a function addWithSurcharge
>> it has two parameters
>> we declare a variable sum
>> we initialize it with a value 
>> this value will be the sum of the two parameters which are numbers
>>> we then open our first if / else if / else statement 
>>>> our first condition is to check if the first digit is less than or equal to 10
>>>> if it meets the condition then we shall be adding 1 to the sum
>>>> our second condition is to check if the first digit is less than or equal to 20  
>>>> if it meets the condition then we shall be adding 2 to the sum
>>>> our final condition will be to add 3 to the sum; if the first number is above 20
>>> we then open our second if / else if / else statement 
>>>> our first condition is to check if the second digit is less than or equal to 10
>>>> if it meets the condition then we shall be adding 1 to the sum
>>>> our second condition is to check if the second digit is less than or equal to 20  
>>>> if it meets the condition then we shall be adding 2 to the sum
>>>> our final condition will be to add 3 to the sum; if the second number is above 20
> the second solution is fairly simple too 
>> we use two separate functions to get our code to successfully execute 
>> we first declare a function lineItemToSurcharge
>> it has only one parameter which will be a number
>> we then open our if / else if / else statement in this function
>> we check to see if the number is less than equal to 10, 20 or above 20
>> similar to what we did in the first solution
>> we then declare another function addWithSurcharge
>> this function has 2 parameters that are numbers
>> we simply add the two numbers first 
>> we declare a variable 'sum' and store the result of the addition of the two numbers in it
>> we then call the function lineItemToSurcharge within this function 
>> we call the function lineItemToSurcharge with the arguments being the two numbers declared as parameters in the function addWithSurcharge
>> we simultaneously add the result of the function lineItemToSurcharge to the  'sum' variable
>> we then return that entire addition 

No comments:

Post a Comment