I was wondering how to print the return value of my function outside of the scope of the function. I researched online and found the post below bu this just gave me arr is undefined. Can somebody please point me in the right direction? Console log not printing variable from function
function multiplyAll(arr) {
var product = 1;
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++)
product = product * arr[i][j];
}
//why is the product undefined?
return product;
}
//modify values below to test code
multiplyAll([
[60, 60],
[24, 365]
]);
//console.log("Print results of product" + product);
Instead of just calling multiplyAll
by itself, you should assign it to a variable like this:
var product = multiplyAll([[60,60],[24,365]]);
Another thing you can do if you only want to print out the result is to just put the function call inside of your console.log
call like so:
console.log("Print results of product" + multiplyAll([[60,60],[24,365]]));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.