i like to ask for advise how to let this array [1,2,3,4] to become 12, 23, 34?
this initial is a number 1234, i split them
let sum
sum = String(this.numbers).split("").map(Number);
console.log('numbers set1', sum)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
so from here it becomes [1,2,3,4]
how can i join/combine/concat then to become 12, 23, 34 respectively?
thank you very much for Hello World's guidance
thank you
CodePudding user response:
You may want to write simple algorithm to get pairs of those numbers. Try to traverse the string/list and insert the new values into another list.
Sudo code can be something like: let's say you have arr with numbers [1,2,3,4]
for i = 1 to N:
string sum = arr[i-1] arr[i];
list.push(sum)
CodePudding user response:
You can easily achieve the result using split
, map
, and filter
const num = 1234;
const result = String(num)
.split("")
.map((n, i, src) => (src.length !== i 1 ? `${n}${src[i 1]}` : null))
.filter(Boolean)
.map(Number);
console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>