anyArray.reduce( (a,b) => a + b,0 ); This formula will sum up all arrays values. AnyArray is the location of the array, it can be in an object, or just a regular Array.
letpu=document.querySelector("#pushups");letsb=document.querySelector("#submit")sb.addEventListener("click",()=>{letnum=+pu.value;//Changes String into a number with the +//this if statament returns nothing if user inputs a non digit numberif(!num){return;}//call coolObjletisCoolObj=coolObj();trackPushups(num,isCoolObj);});//create an Obj inside a functionfunctioncoolObj(){letWeek={Sunday:0,Monday:[{numberOfPushups:[1],numberOfSitups:[],}],Tuesday:2,Wednesday:[{numberOfPushups:[],numberOfSitups:[],}],}returnWeek;}//this function tracks the number of pushups the user entersfunctiontrackPushups(numberOfP,whateverObj){switch(getDay()){case0:console.log("Today is Sunday");break;case1:console.log("Today is Monday");break;case2:console.log("Today is Tuesday");break;case3:console.log("Today is Wednesday");whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)console.log(numberOfP)break;case4:console.log("Today is Thursday");break;case5:console.log("Today is Friday");break;case6:console.log("Today is Saturday");break;}}functiongetDay(){constdate=newDate();letday=date.getDay();returnday;}//https://jsfiddle.net/hrop9gn2/2/
Line: 25 function coolObj() returns Week Object.
Line 18 We store the object into this variable: let isCoolObj = coolObj();
Line 19 We pass the object into trackPushups(num,isCoolObj);
Line 61: function trackPushups(numberOfP,whateverObj) WhateverOBJ is the same as isCoolObj.
Line 79: We store the userInput, and we use PUSH to enter the input into the Object Array whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)
letpu=document.querySelector("#pushups");letsb=document.querySelector("#submit")letarr= [];letisCoolObj=coolObj();sb.addEventListener("click",()=>{letnum=+pu.value;//Changes String into a number with the +//this if statament returns nothing if user inputs a non digit numberif(!num){return;}//call coolObjletsumPushups=trackPushups(num,isCoolObj);console.log(sumPushups+"sumPushups")console.log(isCoolObj.Wednesday[0].numberOfPushups)});//create an Obj inside a functionfunctioncoolObj(){letWeek={Sunday:0,Monday:[{numberOfPushups:[1],numberOfSitups:[],}],Tuesday:2,Wednesday:[{numberOfPushups:[],numberOfSitups:[],}],}returnWeek;}//this function tracks the number of pushups the user entersfunctiontrackPushups(numberOfP,whateverObj){letsum=0;switch(getDay()){case0:console.log("Today is Sunday");break;case1:console.log("Today is Monday");break;case2:console.log("Today is Tuesday");break;case3:console.log("Today is Wednesday");whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)sum=whateverObj.Wednesday[0].numberOfPushups.reduce( (a,b)=>a+b,0);break;case4:console.log("Today is Thursday");break;case5:console.log("Today is Friday");break;case6:console.log("Today is Saturday");break;}returnsum;}functiongetDay(){constdate=newDate();letday=date.getDay();returnday;}console.log(arr)//https://jsfiddle.net/hrop9gn2/4/
Line 4: We need to create the object before it can be accessible.
Line 83: We store the the sum in the sum variable. sum = whateverObj.Wednesday[0].numberOfPushups.reduce( (a,b) => a + b,0);
Line 20: We store the return statement of the function trackPushups(num,isCoolObj);. let sumPushups = trackPushups(num,isCoolObj);