I have a callback function which takes two parameter and a global variable that I would like to add to with the value returned from this function. However the variable remains the same. Please help!
let sum = 0;
const myArr = [{name: 'dj', value: '2'}, {name: 'kd', value: '3'}];
function addObjValue(arr, total) {
arr.forEach(element => {
const val = element.value;
total += checkObjValue(val);
console.log(total);
})
}
function checkObjValue(x) {
switch(x) {
case '2':
return 1;
break;
case '3':
return 5;
break;
}
}
addObjValue(myArr, sum); // sum remains 0