Home > database >  Call a result of a function inside a function
Call a result of a function inside a function

Time:10-27

I want to bring out the results of a function that is within another function. When I try to print or return results it only brings out the result of the function "Sum".

let readlineSync = require("readline-sync");
let a = readlineSync.question(
"Choose an operation: Sum or Substraction: "
);
let param1 = parseInt(readlineSync.question("Value 1: "));
let param2 = parseInt(readlineSync.question("Value 2: "));
chosenFunction();

function Sum() {
    return param1   param2;

}

function Substraction() {
    return param1 - param2;
}

function chosenFunction() {
    let result;
    if (a = 'Sum') {
        result = console.log (Sum());
    } else if (a = 'Substraction') {
        result = console.log ( Substraction());
    }
    return result
}

CodePudding user response:

You need to use == or ===. You are actually changing your values, not comparing, when using = as in else if (a = 'Substraction').

if (a === 'Sum')
else if (a === 'Substraction')

CodePudding user response:

It's an invalid usage,when you assign value,you need to remove console.log

Also need to change = to == when compare values

So change

result = console.log (Sum());
result = console.log ( Substraction());

to

result = Sum();
result = Substraction();

Full code

function chosenFunction() {
    let result;
    if (a == 'Sum') {
        result = Sum();
    } else if (a == 'Substraction') {
        result = Substraction();
    }
    return result
}
  • Related