How can I print label if the corresponding function is called in the console.log. Now its not printing the label and result ? Can someone please advise
let num1 = 100;
let num2 = 200;
let text = 'add';
calculationOfNumbers(text, num1, num2);
function calculationOfNumbers(text, num1, num2) {
if (text == 'add') {
let label = "Adding";
let result = addFunction(num1, num2);
console.log(`This is : ${label}` result);
} else if (text == 'subtract') {
let label = "Subtracting";
let result = subtractFunction(num1, num2);
console.log(`This is : ${label}` result);
}
}
function addFunction(num1, num2 ) {
let result = parseInt(num1) parseInt(num2);
return result;
}
function subtractFunction(num1, num2) {
let result = parseInt(num2) - parseInt(num1) ;
return result;
}
CodePudding user response:
those return label, result
does nothing. It is the same as doing return result
. Learn about comma operator. There's no tuple data type in JS yet.
What you could do instead is the following:
function addFunction(num1, num2, label) {
let result = parseInt(num1) parseInt(num2);
return [label, result];
}
function calculationOfNumbers(x, num1, num2) {
let label, result;
if (x == 'add') {
[label, result] = addFunction(num1, num2, "adding");
} else if (x == 'subtract') {
[label, result] = subtractFunction(num1, num2, "subtracting");
}
console.log(`This is : ${label} ${result}`);
}
ie., return arrays and use destructuring.
Or even:
function calculationOfNumbers(x, num1, num2) {
let result;
if (x == 'add') {
result = addFunction(num1, num2);
} else if (x == 'subtract') {
result = subtractFunction(num1, num2);
}
console.log(`This is: ${label} ${result}`);
}
function addFunction(num1, num2) {
let result = parseInt(num1) parseInt(num2);
return label="adding", result;
}
function subtractFunction(num1, num2) {
let result = parseInt(num2) - parseInt(num1) ;
return label="subtracting", result;
}
(I'm not saying this is the best approach tho)
CodePudding user response:
Since you cannot return multiple values with one return
statement in JavaScript, one option you could use is to instead return an object
like so:
let total = 0;
let num1 = 100;
let num2 = 200;
let text = 'add';
let label = "";
calculationOfNumbers(text, num1, num2);
function calculationOfNumbers(x, num1, num2) {
let info;
if (x == 'add') {
info = addFunction(num1, num2, "adding");
} else if (x == 'subtract') {
info = subtractFunction(num1, num2, "subtracting");
}
console.log(`This is ${info.label} : ${info.result}`);
}
function addFunction(num1, num2, label) {
let result = parseInt(num1) parseInt(num2);
return { label, result };
}
function subtractFunction(num1, num2, label) {
let result = parseInt(num2) - parseInt(num1);
return { label, result };
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Notice how I assign each function call to the variable info
then you can access the required properties with info.label
and info.results
, respectively.
CodePudding user response:
there are few things needs to be amended in your code
function calculationOfNumbers(x, num1, num2) {
if (x == 'add') {
// function is returning some values, but it was never stored somewhere to be used later
addFunction(num1, num2, "adding");
} else if (x == 'subtract') {
// same situation, function is returning some values, but it was never stored somewhere to be used later
subtractFunction(num1, num2, "subtracting");
}
// you are printing (result) but it was never declared before
console.log(`This is : ${label}` result);
}
function addFunction(num1, num2, label) {
// it's better to use Number(num1) instead of parseInt()
let result = parseInt(num1) parseInt(num2);
// you are returning label inside here, however you didn't amend the global variable result,
// also no need to pass it down to this function, the add function should be returning only the result of the calculation
return label, result;
}
function subtractFunction(num1, num2, label) {
let result = parseInt(num2) - parseInt(num1) ;
return label, result;
}
So the code after amendment should look like below:
let num1 = 100;
let num2 = 200;
let text = 'add';
calculationOfNumbers(text, num1, num2);
function calculationOfNumbers(x, num1, num2) {
let label = x;
let result = 0;
if (x == 'add') {
result = addFunction(num1, num2);
} else if (x == 'subtract') {
result = subtractFunction(num1, num2);
}
console.log(`This is : ${label}` result);
}
function addFunction(num1, num2) {
let result = Number(num1) Number(num2);
return result;
}
function subtractFunction(num1, num2) {
let result = Number(num2) - Number(num1);
return result;
}