Javascript String removeAt(index)

Description

Javascript String removeAt(index)


String.prototype.removeAt = function(index) {
  return this.substring(0, index) + 
    this.substring(index + 1, this.length)
}

const removeWhiteSpaces = string => {
  
  let read = 0// w  ww  .  j  a v  a  2 s  . c  o  m
  let write = 0

  for (var i = 0; i < string.length; i++) {
    var curr = string[i]
    if (curr === ' ') {
      string = string.removeAt(i)
      i--
    }
  }

  return string

}

const tests = [
  {i: ' hello  world', o: 'helloworld'},
  {i: 'a b c d', o: 'abcd'},
  {i: '  ', o: ''},
  {i: '   a     ', o: 'a'},
]

tests.forEach((test, i) => {
  const output = removeWhiteSpaces(test.i)
  const result = output === test.o ? 'PASSED' :
    `FAILED - expected ${output} to equal ${test.o}`
  console.log(`Test ${i+1}: ${result}`)
})



PreviousNext

Related