We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I did about the same but used some methods in the function for easy refactoring just in case the sub-logic ever needed to be used for another purpose or more fruit types needed to be added
functioncountApplesAndOranges(s,t,a,b,apples,oranges){constfLoc=function(treeLoc,arr2d){returnarr2d.map(fruitLoc=>(treeLoc+fruitLoc));}constfRange=function(s,t,arr2d){leta,b;a=0;b=0;arr2d.forEach((f,i)=>{if(i===0){// apple countf.forEach(loc=>s<=loc&&loc<=t?a++:null);}if(i===1){// orange countf.forEach(loc=>s<=loc&&loc<=t?b++:null);}});return[a,b];}console.log(fRange(s,t,[fLoc(a,apples),fLoc(b,oranges)]).join('\n'));}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Apple and Orange
You are viewing a single comment's thread. Return to all comments →
I did about the same but used some methods in the function for easy refactoring just in case the sub-logic ever needed to be used for another purpose or more fruit types needed to be added