The first result is the solution, not quite sure why it prints so many other results after that even if I set matchFound = true though 🤔
constdata=document.querySelector('pre').innerTextconstfrequencies=data.split('\n')letsums=[]functiongetFrequency(freqs,acc=0,callback){returnfreqs.reduce((acc,val)=>{constsign=val[0]==='+'?1:-1constnumber=val.substring(1)constcurrent=acc+sign*numbercallback(current)returncurrent},acc)}letstartFreq=getFrequency(frequencies,0,current=>sums.push(current))functionisMatch(arr,el){returnarr.find(element=>element===el)}functionfindMatch(){letmatchFound=falsewhile(!matchFound){startFreq=getFrequency(frequencies,startFreq,current=>{if(isMatch(sums,current)){matchFound=trueconsole.log('The match is',current)}})}}findMatch()
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
1.1
A bit overcomplicated. Didn't know parseInt parses signs too 😅
1.2
The first result is the solution, not quite sure why it prints so many other results after that even if I set matchFound = true though 🤔