Search code examples
javascriptstringfunctionnumberspalindrome

Сoncatenation FiizBuzz


Create a function called “sum”, that takes two arguments: a number, represented as string, and a number. If an argument of type number is divisible by 3, 5 and 15 without remainder, multiply it by -1. Function should return numeric sum of two arguments.

My solution is not full, help to understand what should i do

const sum = (value1, value2) => {
   for(let i = 0; i < value1, value2; i++) {
     if(i % 3 === 0 && i % 5 === 0 && i% 15 === 0) {
       return value1 + value2
     }
   }
    return;
  };

Examples:

sum('25',15) equals 10, because 15 is divisible by 3, 5 and 15 and therefore the sum is 25 + (-15)
sum('41',3) equals 44, because neither 41 nor 3 are divisible by 3,5, nor 15 and the sum is 41 + 3


Solution

  • Here is a solution based on the expected output given:

    const sum = (value1, value2) => {
      var result = 0;
      const arr = [value1, value2];
      arr.forEach(function(value) {
        if ((typeof value === 'number') && value % 15 === 0) {
          result += value * (-1);
        } else result += Number(value);
      });
      return result;
    }
    
    
    console.log(sum('25', 15));
    console.log(sum(41, '3'));
    console.log(sum('3', 45));
    console.log(sum('15', 15));
    console.log(sum(3, 15));