1

Can you tell me please, why the final array does not have the last item of the initial array; it returns [3, 6, 9, 2, 4, 6], and we are missing '8'.

let item = [123,456,789,12,34,56,78];
const tailAndHead = arr => arr.slice(1).reduce((a,v,i) => (a.push(arr[i]%10), a),  []);
0

3 Answers 3

3

Why not use Array#map?

tailAndHead = arr.map(a => a % 10);
Sign up to request clarification or add additional context in comments.

1 Comment

You can just use a instead of arr[i]
2

Remove slice(1).

let item = [123,456,789,12,34,56,78];

const tailAndHead = arr => arr.reduce((a,v,i) => (a.push(arr[i]%10), a),  []);

Comments

1

You should push the sliced array item. Please modify you code like this.

let item = [123,456,789,12,34,56,78];
const tailAndHead = arr => arr.slice(1).reduce((a,v,i,b) => (a.push(b[i]%10), a),  []);
console.log(tailAndHead(item));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.