-2
function splitCamelCase(string) {
  return string.split(/([A-Z])/g)
  .map(function(x,i) {
    if (x === x.toUpperCase()) {
      x[i+1] = x + x[i+1]
      x = " "
      
    }
    
  })
.join('')
}

This code is supposed to split camel case text at each uppercase letter, but it just turns every item of the array to undefined As far as I understand nothing here is changing the values to something they shouldnt be

Tried changing "x[i+1] = x + x[i+1]" to " = ${x}${x[i+1]}, nothing happened Tried specifying the array in the map function parameters, notthing changed

xzemiyl
  • 1
  • 3
  • Your `.map()` callback function doesn't return anything. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#parameters – Phil Mar 14 '23 at 01:05
  • where is the return? – epascarello Mar 14 '23 at 01:15
  • Functions in javascript returns `undefined` when you don't return anything. While other languages like Java and C++ have the `void` type for functions that don't return anything in javascript "nothingness" actually have a value and it's `undefined` – slebetman Mar 14 '23 at 01:37
  • also [When should I use a return statement in ES6 arrow functions](https://stackoverflow.com/questions/28889450/when-should-i-use-a-return-statement-in-es6-arrow-functions) if you choose to use an arrow function instead – pilchard Mar 14 '23 at 01:52

1 Answers1

0

The issue you've got is you're not returning anything in the map.

That said, there's a simpler way to achieve the result you're after:

function splitCamelCase(string) {
   return string.split(/(?=[A-Z])/).join(" ");
}

Javascript Split string on UpperCase Characters

John Detlefs
  • 952
  • 8
  • 16