From d15ef2afa989d7f4477bb27e1e0494aae06e5505 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Fri, 15 Dec 2017 00:23:10 +0530 Subject: [PATCH 1/5] Added snippet to convert number to array of digits --- snippets/number-to-array-of-digits.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/number-to-array-of-digits.md diff --git a/snippets/number-to-array-of-digits.md b/snippets/number-to-array-of-digits.md new file mode 100644 index 000000000..710540762 --- /dev/null +++ b/snippets/number-to-array-of-digits.md @@ -0,0 +1,17 @@ +### Number to array to digits + +use `parseInt()` to get only the integer part of the quotient. +use `Array.reverse()` to return the array in the same order as the number. + +```js +const num2array = (n) =>{ + let arr = []; + while (n>0) { + arr.push(n%10); + n = parseInt(n/10); + } + return arr.reverse(); +} + +// num2array(2334) -> [2, 3, 3, 4] +``` \ No newline at end of file From 655584d0ed8d6dc91c0a805c40a8a581da41bdc3 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Fri, 15 Dec 2017 00:25:46 +0530 Subject: [PATCH 2/5] Fixed typo --- snippets/number-to-array-of-digits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/number-to-array-of-digits.md b/snippets/number-to-array-of-digits.md index 710540762..aa4652fa7 100644 --- a/snippets/number-to-array-of-digits.md +++ b/snippets/number-to-array-of-digits.md @@ -1,4 +1,4 @@ -### Number to array to digits +### Number to array of digits use `parseInt()` to get only the integer part of the quotient. use `Array.reverse()` to return the array in the same order as the number. From 6ef96cc3a616f599517bc32801a177bc66538608 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Fri, 15 Dec 2017 00:37:32 +0530 Subject: [PATCH 3/5] updated changes --- snippets/number-to-array-of-digits.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/snippets/number-to-array-of-digits.md b/snippets/number-to-array-of-digits.md index aa4652fa7..63fe6c907 100644 --- a/snippets/number-to-array-of-digits.md +++ b/snippets/number-to-array-of-digits.md @@ -5,12 +5,9 @@ use `Array.reverse()` to return the array in the same order as the number. ```js const num2array = (n) =>{ - let arr = []; - while (n>0) { - arr.push(n%10); - n = parseInt(n/10); - } - return arr.reverse(); + return (''+n).split('').map((i) =>{ + return parseInt(i); + }) } // num2array(2334) -> [2, 3, 3, 4] From f827f0d1090bf77f8940f59727a4cca0027d3456 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Fri, 15 Dec 2017 00:44:39 +0530 Subject: [PATCH 4/5] updated changes --- snippets/number-to-array-of-digits.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/number-to-array-of-digits.md b/snippets/number-to-array-of-digits.md index 63fe6c907..9493c279f 100644 --- a/snippets/number-to-array-of-digits.md +++ b/snippets/number-to-array-of-digits.md @@ -1,7 +1,7 @@ ### Number to array of digits -use `parseInt()` to get only the integer part of the quotient. -use `Array.reverse()` to return the array in the same order as the number. +Convert the number to a string, split the string using `split()`. +use `parseInt()` to convert every element back to integer. ```js const num2array = (n) =>{ From 69b3e2b97fa775cfae4e35462303e3e7b7856c59 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 22:53:42 +0200 Subject: [PATCH 5/5] Update number-to-array-of-digits.md --- snippets/number-to-array-of-digits.md | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/snippets/number-to-array-of-digits.md b/snippets/number-to-array-of-digits.md index 9493c279f..f00a62b75 100644 --- a/snippets/number-to-array-of-digits.md +++ b/snippets/number-to-array-of-digits.md @@ -1,14 +1,9 @@ ### Number to array of digits -Convert the number to a string, split the string using `split()`. -use `parseInt()` to convert every element back to integer. +Convert the number to a string, use `split()` to convert build an array. +Use `Array.map()` and `parseInt()` to transform each value to an integer. ```js -const num2array = (n) =>{ - return (''+n).split('').map((i) =>{ - return parseInt(i); - }) -} - -// num2array(2334) -> [2, 3, 3, 4] -``` \ No newline at end of file +const digitize = n => (''+n).split('').map(i => parseInt(i)); +// digitize(2334) -> [2, 3, 3, 4] +```