-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new directory Hacktoberfest-2021/FirdausRazali/js-exercise and …
…an exercise001.js file in it
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Hacktoberfest-2021/FirdausRazali/js-exercise/exercise001.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ |