Skip to content

Commit

Permalink
Added new directory Hacktoberfest-2021/FirdausRazali/js-exercise and …
Browse files Browse the repository at this point in the history
…an exercise001.js file in it
  • Loading branch information
matyod committed Sep 8, 2023
1 parent 47d32e3 commit 42a7a63
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Hacktoberfest-2021/FirdausRazali/js-exercise/exercise001.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Stand in Line
In Computer Science a queue is an abstract Data Structure where items are kept in order.
New items can be added at the back of the queue and old items are taken off from the front of the queue.
Write a function `nextInLine` which takes an array (`arr`) and a number (`item`) as arguments.
Add the number to the end of the array, then remove the first element of the array.
The `nextInLine` function should then return the element that was removed.
Display the initial array, the return element, and the modified array.
*/

/*
pop = remove last element
push = add last element
shift = remove first element
unshift = add first element
*/

let testArr = [1, 2, 3, 4, 5]; // initialize array

function nextInLine(arr, item){
arr.push(item);// add item to end of array
return arr.shift();// remove first element from array
}

console.log("Before: " + JSON.stringify(testArr));
console.log("Removed element: " + JSON.stringify(nextInLine(testArr, 6)));
console.log("After: " + JSON.stringify(testArr));

/*
JSON.stringify== is used to convert array or object (in this case, array testArr) to string for display purpose.
This can be useful when want to transmit or store data in a text-based format,
such as when working with APIs or saving data to a file.
*/

0 comments on commit 42a7a63

Please sign in to comment.