Codewars - convert a string to an array solution

Problem link

Solution one

Just split the string and it will return a array. This solution is in JavaScript

function stringToArray(string) {
  return string.split(' ')
}

Solution two

In Python we can turn the string to an list and the job is done.

function stringToArray(string) {
  return list(string)
}

Solution three

We can use for loop to do our job.

function stringToArray(string) {
  arr = []

  for i in string:
    arr.append(i)

  return arr
}

Solution four

This solution is same as above but it’s one liner.

function stringToArray(string) {
  return [i for i in string]
}

Solution five

Now it’s little bit fancier. We are using Python destructuring here.

function stringToArray(string) {
  return [*string]
}
IMRAN POLLOB
IMRAN POLLOB
PhD Student

Fullstack software engineer with 4+ years of experiece learing to teach machines