-
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.
Merge pull request #108 from ausrazali/add-js-exercise-files
Add js exercise files
- Loading branch information
Showing
2 changed files
with
83 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. | ||
*/ |
45 changes: 45 additions & 0 deletions
45
Hacktoberfest-2021/FirdausRazali/js-exercise/exercise002.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,45 @@ | ||
/* | ||
Golf Code | ||
In the game of Golf, each hole has a `par`, meaning, the average number of `strokes` | ||
a golfer is expected to make in order to sink the ball in the hole to complete the play. | ||
Depending on how far above or below `par` your `strokes` are, there is a different nickname. | ||
Your function will be passed `par` and `strokes` arguments. | ||
Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest): | ||
Strokes Return | ||
1 "Hole-in-one!" | ||
<= par - 2 "Eagle" | ||
par - 1 "Birdie" | ||
par "Par" | ||
par + 1 "Bogey" | ||
par + 2 "Double Bogey" | ||
>= par + 3 "Go Home!" | ||
`par` and `strokes` will always be numeric and positive. We have added an array of all the names for your convenience. | ||
`const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]; | ||
*/ | ||
|
||
const names=["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]; | ||
|
||
function golfScore(par, strokes){ | ||
if (strokes === 1) { | ||
return names[0]; | ||
} else if (strokes <= par - 2){ | ||
return names[1]; | ||
} else if (strokes === par - 1){ | ||
return names[2]; | ||
} else if (strokes === par){ | ||
return names[3]; | ||
} else if (strokes === par + 1){ | ||
return names[4]; | ||
} else if (strokes === par + 2){ | ||
return names[5]; | ||
} else if (strokes >= par + 3){ | ||
return names[6]; | ||
} | ||
} | ||
|
||
console.log(golfScore(5, 4)); |