Search code examples
javascriptarraysfunctionmath

my mathematical functions are returning NaN


so I have been asking to change my code to simplify my functions but now that i have simpliftied them they are now only returning NaN and i dont really understand why that is, when the input is numbers?

// the purpose of this script is to execute different functions on an array of 3 integers// 
const input = [6, 25, 11];
const findSum = input.reduce((acc, num) => acc + num, 0);
console.log(findSum);

// each function has been assigned parameters to execute //
const subsFrom = (num1, num2, num3) => num1 - num2 - num3;
const subNum = subsFrom(input);
console.log(subNum);
// then call the function and display the answer //

const multiBy = (num1, num2, num3) => num1 * num2 * num3;
const multiNum = multiBy(input)
console.log(multiNum);

const divBy = (findSum, num1, num2, num3) => findSum / num1 / num2 / num3;
const divNum = divBy(findSum, input);
console.log(divNum);


Solution

  • When destructuring the array as params, you need to add the square brackets in the param list.

    const subsFrom = ([num1, num2, num3])
    const multiBy = ([num1, num2, num3])
    const divBy = (findSum, [num1, num2, num3])
    

    Since input is an array being passed into the function call.

    // the purpose of this script is to execute different functions on an array of 3 integers// 
    const input = [6, 25, 11];
    const findSum = input.reduce((acc, num) => acc + num, 0);
    console.log(findSum);
    
    // each function has been assigned parameters to execute //
    const subsFrom = ([num1, num2, num3]) => num1 - num2 - num3;
    const subNum = subsFrom(input);
    console.log(subNum);
    // then call the function and display the answer //
    
    const multiBy = ([num1, num2, num3]) => num1 * num2 * num3;
    const multiNum = multiBy(input)
    console.log(multiNum);
    
    const divBy = (findSum, [num1, num2, num3]) => findSum / num1 / num2 / num3;
    const divNum = divBy(findSum, input);
    console.log(divNum);

    Alternatively, you can spread the input array values into the function call. This way you do not need to add the square brackets to destructure the incoming array.

    const divBy = (findSum, num1, num2, num3) => findSum / num1 / num2 / num3;
    const divNum = divBy(findSum, ...input); // Spread the array values
    

    // the purpose of this script is to execute different functions on an array of 3 integers// 
    const input = [6, 25, 11];
    const findSum = input.reduce((acc, num) => acc + num, 0);
    console.log(findSum);
    
    // each function has been assigned parameters to execute //
    const subsFrom = (num1, num2, num3) => num1 - num2 - num3;
    const subNum = subsFrom(...input);
    console.log(subNum);
    // then call the function and display the answer //
    
    const multiBy = (num1, num2, num3) => num1 * num2 * num3;
    const multiNum = multiBy(...input)
    console.log(multiNum);
    
    const divBy = (findSum, num1, num2, num3) => findSum / num1 / num2 / num3;
    const divNum = divBy(findSum, ...input);
    console.log(divNum);