0

I have string "513". I need array ["5", "1", "3"]. My solution:

function nextBigger(num){
    let numStr = '' + num;
  let numArr = [];

  for(let i = 0; i < numStr.length; ++i) {
    numArr.push(numStr[i]);
  }

  console.log(numArr);
}

nextBigger(513);

But this solution is large and redundant. I need shorter solution.

super.prozandr1
  • 107
  • 3
  • 10

5 Answers5

8

With ES6, you could use the spread syntax ... which takes an iterable and iterates the single items.

Or just take the power of Array.from, which does nearly the same (and much more).

const getCharacters1 = string => [...string];
const getCharacters2 = string => Array.from(string);

console.log(getCharacters1('513'));
console.log(getCharacters2('513'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
6

You can just .split() with an empty string as the delimiter. This will split the string at each character:

function nextBigger(num){
  console.log(num.toString().split(""));
}

nextBigger(513);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 1
    "on an empty string" ? – Adrian Nov 16 '17 at 14:58
  • @Adriani6 Yes, `""` is an empty string. – Scott Marcus Nov 16 '17 at 14:58
  • @Adriani6 are you asking what an empty string is? –  Nov 16 '17 at 14:58
  • @ScottMarcus Ah right, of course. @Amy No, I just got confused, I was referring to `.toString()` to being an empty string. All sorted now, cheers :) – Adrian Nov 16 '17 at 15:00
  • This works because [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split): "Tip: If an empty string ("") is used as the separator, the string is split between each character." –  Nov 16 '17 at 15:01
  • No no, I know how it works thanks! :) I was just confused with the terminology used in this context, I got it all clear now :) – Adrian Nov 16 '17 at 15:02
  • 1
    Sorry for the misunderstanding, that wasn't addressed to you. I wanted to expand Scott's answer with some documentation. –  Nov 16 '17 at 15:04
  • @GolezTrol Scott didn't ask the question. What are you talking about? –  Nov 16 '17 at 15:05
  • 1
    @GolezTrol i disagree with you wholeheartedly, this was a good answer. If you think this question deserves a higher quality answer, why don't you write one? Show us how it's done. –  Nov 16 '17 at 15:10
  • 1
    @Amy A better answer already exist (*note `.split('')` has issues that spread syntax solves*), it's in the linked duplicate. While this answer is fine, I see where *GolezTrol* is coming from. Generally there isn't a reason to answer a common question with a well known duplicate. – Spencer Wieczorek Nov 16 '17 at 15:14
  • @GolezTrol This has nothing to do with the quality of the answer, which was your initial criticism. The goal post has moved though, I see. –  Nov 16 '17 at 15:18
  • @Amy I thought that was implied when I wrote *"And that for a very simple question that must have been asked a dozen times at least."*, but apparently that was too vague. It certainly wasn't my intention to get in a fight like this or create this huge discussion. I pleaded for quick removal of a duplicate. If the response is to rather choose rep over quality, there is not much I can do, so I'll just remove my part of this useless discussion, and I hope you take care of yours. Business as usual. Don't know what I was thinking. Sorry. – GolezTrol Nov 16 '17 at 16:07
2

You can do it by numString.split(''). (Two single quotes without space). That will return an array with all the characters.

Raphael Jenni
  • 318
  • 1
  • 8
2

Maybe use .split() function.

"513".split("")
Krzysztof Mazur
  • 568
  • 2
  • 13
2

You can use the Spread syntax if you want something really short

let a = [...'513']

Radu Chiriac
  • 1,374
  • 3
  • 18
  • 34