A small Node.js practice exercise covering the most common JavaScript array methods. Only the following methods are used: push, pop, unshift, shift, includes, indexOf, lastIndexOf, concat, slice, splice, sort, reverse.
The script walks through five focused tasks:
- Adding and Removing Elements —
push,shift,unshiftto mutate a fruits array. - Query and Access —
includes,indexOf,lastIndexOfto inspect a colors array. - Combining Arrays —
concatandpushto merge two team arrays. - Extracting and Splicing —
sliceto extract a subrange (non-mutating) andspliceto replace elements (mutating). - Sorting and Reversing —
sort((a, b) => a - b)followed byreverse()for descending order.
Prerequisites: Node.js installed.
# from the project root
node index.jsExpected output:
Task 1: [ 'grape', 'banana', 'cherry', 'orange' ]
Task 2: [ true, 1, 3 ]
Task 3: [ 'Alice', 'Bob', 'Charlie', 'Diana', 'Eve' ]
Task 4 middleNumbers: [ 20, 30 ]
Task 4 numbers: [ 10, 20, 30, 60, 70 ]
Task 5: [ 95, 85, 75, 70, 60 ]