-
Notifications
You must be signed in to change notification settings - Fork 22
Solution #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mahdi209
wants to merge
5
commits into
TheCodePeople:solution
Choose a base branch
from
Mahdi209:solution
base: solution
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #10
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,74 @@ | ||
| /************************************************************** | ||
| Task 8 (logArray): | ||
| Create a function logArray that receives an array as a parameter and logs each item in the array. | ||
| **************************************************************/ | ||
| function logArray(arr) { | ||
| arr.forEach((value, index) => { | ||
| console.log(value); | ||
| }); | ||
| } | ||
|
|
||
| // logArray([1, 2, 3, 4, 5]); | ||
|
|
||
| /************************************************************** | ||
| Task 9 (logArrayWithIndex): | ||
| Create a function logArrayWithIndex that receives an array as a parameter and logs each item in the array along with its index. | ||
| **************************************************************/ | ||
| function logArrayWithIndex(arr) { | ||
| arr.forEach((value, index) => { | ||
| console.log(index); | ||
| }); | ||
| } | ||
|
|
||
| // logArrayWithIndex(["apple", "banana", "orange"]); | ||
|
|
||
| /************************************************************** | ||
| Task 10 (logEvenNumbers): | ||
| Create a function logEvenNumbers that receives an array of numbers as a parameter and logs only the even numbers in the array. | ||
| **************************************************************/ | ||
| function logEvenNumbers(arr) { | ||
| let evenNumber = arr.filter((value, index) => { | ||
| return value % 2 === 0; | ||
| }); | ||
| console.log(evenNumber); | ||
| } | ||
|
|
||
| // logEvenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
| /************************************************************** | ||
| Task 11 (logArrayBackwards): | ||
| Create a function logArrayBackwards that receives an array as a parameter and logs each item in the array in reverse order. | ||
| **************************************************************/ | ||
| function logArrayBackwards(arr) { | ||
| const backWords = arr.map((value, index) => { | ||
| return arr[arr.length - 1 - index]; | ||
| }); | ||
| console.log(backWords); | ||
| } | ||
|
|
||
| // logArrayBackwards(["one", "two", "three", "four"]); | ||
|
|
||
| /************************************************************** | ||
| Task 12 (logLastItem): | ||
| Create a function logLastItem that receives an array as a parameter and logs the last item in the array. | ||
| **************************************************************/ | ||
| function logLastItem(arr) { | ||
| const lastItem = arr.find((value, index) => { | ||
| return value[arr.length - 1 - index]; | ||
| }); | ||
| console.log(lastItem); | ||
| } | ||
|
|
||
| // logLastItem(["a", "b", "c", "d"]); | ||
|
|
||
| /************************************************************** | ||
| Task 13 (logArrayInChunks): | ||
| Create a function logArrayInChunks that receives an array and a chunk size as parameters and logs the items in the array in chunks of the specified size. | ||
| **************************************************************/ | ||
| function logArrayInChunks(arr, chunkSize) { | ||
| for (let i = 0; i < arr.length; i += chunkSize) { | ||
| const chunk = arr.slice(i, i + chunkSize); | ||
| console.log(chunk); | ||
| } | ||
| } | ||
|
|
||
| // logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); | ||
This file contains hidden or 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,198 @@ | ||
| /************************************************************** | ||
| Task 1: | ||
| Create a function sumArray that receives an array of numbers as a parameter and returns the sum of all the numbers in the array. | ||
| Hint: Use the .reduce() method | ||
| **************************************************************/ | ||
| function sumArray(numbers) { | ||
| //TODO: Add your code here | ||
| return numbers.reduce( | ||
| (accumulator, currentNumber) => accumulator + currentNumber | ||
| ); | ||
| } | ||
| // console.log(sumArray([4, 3, 2, 5, -10])); | ||
|
|
||
| /************************************************************** | ||
| Task 2: | ||
| Create a function findFirstStringStartingWithA that receives an array of strings as a parameter | ||
| and returns the first string that starts with the letter given letter. | ||
|
|
||
| Hint: Use the .find() and .startsWith() methods | ||
| **************************************************************/ | ||
| function findFirstStringStartingWithLetter(letter, strings) { | ||
| //TODO: Add your code here | ||
| return strings.find((value, index) => value.toLowerCase().startsWith(letter)); | ||
| } | ||
| // console.log( | ||
| // findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"]) | ||
| // ); | ||
|
|
||
| /************************************************************** | ||
| Task 3: | ||
| Create a function isPresentIncluded that receives an array of presents as a parameter and uses the includes method to check | ||
| if a present is included in the array. | ||
|
|
||
| Hint: Use the .map() and .includes() methods | ||
| **************************************************************/ | ||
| function isPresentIncluded(presentName, presents) { | ||
| //TODO: Add your code here | ||
| return presents.map((value) => value.toLowerCase()).includes(presentName); | ||
| } | ||
| // console.log( | ||
| // isPresentIncluded("puzzle", [ | ||
| // "Sparkling Surprise", | ||
| // "Enchanted Elegance", | ||
| // "Whimsical Wonder", | ||
| // "Joyful Jingle", | ||
| // "Puzzle", | ||
| // ]) | ||
| // ); | ||
|
|
||
| /************************************************************** | ||
| Task 4: | ||
| Create a function sortStudentsAlphabetically that receives an array of students name as a parameter | ||
| and uses the sort method to sort the strings in alphabetical order. | ||
|
|
||
| Hint: Use the .sort() method | ||
| **************************************************************/ | ||
| function sortStudentsAlphabetically(students) { | ||
| //TODO: Add your code here | ||
| return students.sort(); | ||
| } | ||
| // console.log( | ||
| // sortStudentsAlphabetically([ | ||
| // "Eve", | ||
| // "Jasmia", | ||
| // "Husnia", | ||
| // "Grace", | ||
| // "Bob", | ||
| // "Charlie", | ||
| // "Alice", | ||
| // "Dave", | ||
| // "Um abbas", | ||
| // "Frank", | ||
| // ]) | ||
| // ); | ||
|
|
||
| /************************************************************** | ||
| Task 5: | ||
| Create a function that takes an array of numbers as input, separates the odd and even numbers, and returns two new arrays. | ||
| - Hint: Use the forEach method. | ||
| - const [odds, evens] = separateOddEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
| - console.log(odds); // Output: [1, 3, 5, 7, 9] | ||
| - console.log(evens); // Output: [2, 4, 6, 8, 10] | ||
|
|
||
| Hint: Use the .forEach() and .push() methods | ||
| **************************************************************/ | ||
| function separateOddEven(numbers) { | ||
| //TODO: Add your code here | ||
| let odds = []; | ||
| let evens = []; | ||
| numbers.forEach((value) => { | ||
| if (value % 2 === 0) { | ||
| evens.push(value); | ||
| } else { | ||
| odds.push(value); | ||
| } | ||
| }); | ||
| return { odds, evens }; | ||
| } | ||
| // console.log(separateOddEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); | ||
|
|
||
| /************************************************************** | ||
| Task 6: | ||
| Create a function that takes two parameters: a code that represents an item, and an array of item codes, then removes the item with the given code from the system. | ||
| - Hint: Use the filter method. | ||
| - e.g | ||
| const items = [ | ||
| { code: '#153', name: 'Ball' }, | ||
| { code: '#147', name: 'Scissors' }, | ||
| { code: '#249', name: 'Pillow' }, | ||
| { code: '#149', name: 'Tissue' } | ||
| ]; | ||
|
|
||
| const updatedItems = removeItem('#153', items); | ||
| console.log(updatedItems); // Output: [{ code: "#147", name: "Scissors" }, { code: "#249", name: "Pillow" }, { code: "#149", name: "Tissue" },]; | ||
|
|
||
| Hint: Use the .filter and .startsWith method | ||
| **************************************************************/ | ||
| function removeItem(code, items) { | ||
| //TODO: Add your code here | ||
| return items.filter((value) => { | ||
| value.code !== code; | ||
| }); | ||
| } | ||
| // console.log( | ||
| // removeItem("#153", [ | ||
| // { code: "#153", name: "Ball" }, | ||
| // { code: "#147", name: "Scissors" }, | ||
| // { code: "#249", name: "Pillow" }, | ||
| // { code: "#149", name: "Tissue" }, | ||
| // ]) | ||
| // ); | ||
|
|
||
| /************************************************************** | ||
| Task 7: | ||
| Write a function that takes an array of objects representing students, with properties like firstName, lastName, grade, and | ||
| type (nerd or regular), and a curve which represents the amount of the curve. The function should return a new array with | ||
| updated grades, with nerds receiving a negative curve and regular students receiving a positive curve. | ||
| - Hint: Use the map method. | ||
| - e.g | ||
| const students = [ | ||
| { firstName: "Jaber", lastName: "jabarbar", grade: 10, type: "regular" }, | ||
| { firstName: "Hamza", lastName: "Alhamazi", grade: 12, type: "regular" }, | ||
| { firstName: "Jasem", lastName: "Jamasmas", grade: 15, type: "nerd" }, | ||
| { firstName: "Kadhim", lastName: "Khadhmia", grade: 5, type: "regular" }, | ||
| { firstName: "Um Abbas", lastName: "IDK", grade: 20, type: "nerd" }, | ||
| { firstName: "Johny", lastName: "Micle", grade: 10, type: "regular" }, | ||
| ]; | ||
| const updatedGrades = updateGrades(students, 10); | ||
| console.log(updatedGrades); | ||
|
|
||
| Output: | ||
| [ | ||
| { | ||
| firstName: "Jaber", | ||
| lastName: "jabarbar", | ||
| grade: 20, | ||
| type: "regular", | ||
| }, | ||
| { | ||
| firstName: "Hamza", | ||
| lastName: "Alhamazi", | ||
| grade: 22, | ||
| type: "regular", | ||
| }, | ||
| { firstName: "Jasem", lastName: "Jamasmas", grade: 5, type: "nerd" }, | ||
| { | ||
| firstName: "Kadhim", | ||
| lastName: "Khadhmia", | ||
| grade: 15, | ||
| type: "regular", | ||
| }, | ||
| { firstName: "Um Abbas", lastName: "Winston", grade: 10, type: "nerd" }, | ||
| { firstName: "Johny", lastName: "Micle", grade: 20, type: "regular" }, | ||
| ]; | ||
|
|
||
| Hint: Use the .map method and separator operator | ||
| **************************************************************/ | ||
| function updateGrades(curve, students) { | ||
| //TODO: Add your code here | ||
| return students.map((value) => { | ||
| if (value.type === "nerd") { | ||
| return { ...value, grade: value.grade - curve }; | ||
| } else { | ||
| return { ...value, grade: value.grade + curve }; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // console.log( | ||
| // updateGrades(10, [ | ||
| // { firstName: "Jaber", lastName: "jabarbar", grade: 10, type: "regular" }, | ||
| // { firstName: "Hamza", lastName: "Alhamazi", grade: 12, type: "regular" }, | ||
| // { firstName: "Jasem", lastName: "Jamasmas", grade: 15, type: "nerd" }, | ||
| // { firstName: "Kadhim", lastName: "Khadhmia", grade: 5, type: "regular" }, | ||
| // { firstName: "Um Abbas", lastName: "IDK", grade: 20, type: "nerd" }, | ||
| // { firstName: "Johny", lastName: "Micle", grade: 10, type: "regular" }, | ||
| // ]) | ||
| // ); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Arrays</title> | ||
| </head> | ||
| <body> | ||
| <script src="./arrays.js" type="module" /> | ||
| </body> | ||
| </html> | ||
| </head> | ||
| <body> | ||
| <script src="./reviewers.js" type="module" /> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can easily get the last item without using any methods by just using
arr[arr.length -1]