[Solved] recursion javascript, ask for answer


and totally redeem yourself !

SO likes to punish people if they don’t show their work, but I’m not one to judge. I do think you will learn more if you try these problems on your own, but I know what it’s like to be completely bewildered; and no good-natured soul amongst us wants to see you get stuck.

With that in mind, I really do think you can learn a lot from this answer. Please study it carefully and ask follow-up questions if you’re confused.

const ord = c =>
  c.charCodeAt (0)

const isCharSeq = (x, y) =>
  ord (x) + 1 === ord (y)

const dataReducer = ([x, ...xs], last = null, acc = []) =>
  x === undefined
    ? acc.join (',')
  : last === null
    ? dataReducer (xs, x, acc)
  : isCharSeq (last, x)
    ? dataReducer (xs, x, acc)
  : dataReducer (xs, last, acc.concat ([x]))

console.log (dataReducer ('abcdxefgh5wi')) // 'x,5,w'
console.log (dataReducer ('opqrstu'))      // ''
console.log (dataReducer ('acdefghij'))    // 'c,d,e,f,g,h,i,j'
console.log (dataReducer ('testu'))        // 'e,s,t'
console.log (dataReducer ('a'))            // ''
console.log (dataReducer (''))             // ''

dataReducer is implemented with a proper tail call which means it can be optimised and even work on strings that are millions or billions of characters in length – even if JavaScript VM’s never support this optimisation, don’t worry, you can DIY.

2

solved recursion javascript, ask for answer