-
Notifications
You must be signed in to change notification settings - Fork 22
array tasks are solved #12
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,12 @@ | |
| 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 | ||
| } | ||
| // function sumArray(numbers) { | ||
| // const sum= numbers.reduce((accumulator, currentValue)=>{ | ||
| // return accumulator+currentValue; | ||
| // }) | ||
| // return sum | ||
| // } | ||
| // console.log(sumArray([4, 3, 2, 5, -10])); | ||
|
|
||
| /************************************************************** | ||
|
|
@@ -15,11 +18,13 @@ function sumArray(numbers) { | |
|
|
||
| Hint: Use the .find() and .startsWith() methods | ||
| **************************************************************/ | ||
| function findFirstStringStartingWithLetter(letter, strings) { | ||
| //TODO: Add your code here | ||
| } | ||
| // function findFirstStringStartingWithLetter(letter, strings) { | ||
| // var result = strings.find((value,index) => { | ||
| // return value.startsWith(letter);}) | ||
| // return result | ||
| // } | ||
| // console.log( | ||
| // findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"]) | ||
| // findFirstStringStartingWithLetter("H", ["Memory", "Hello", "Happy"]) | ||
| // ); | ||
|
|
||
| /************************************************************** | ||
|
|
@@ -29,11 +34,11 @@ function findFirstStringStartingWithLetter(letter, strings) { | |
|
|
||
| Hint: Use the .map() and .includes() methods | ||
| **************************************************************/ | ||
| function isPresentIncluded(presentName, presents) { | ||
| //TODO: Add your code here | ||
| } | ||
| // function isPresentIncluded(presentName, presents) { | ||
| // return presents.includes(presentName) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you're supposed to use the |
||
| // } | ||
| // console.log( | ||
| // isPresentIncluded("puzzle", [ | ||
| // isPresentIncluded("blabla", [ | ||
| // "Sparkling Surprise", | ||
| // "Enchanted Elegance", | ||
| // "Whimsical Wonder", | ||
|
|
@@ -49,9 +54,10 @@ function isPresentIncluded(presentName, presents) { | |
|
|
||
| Hint: Use the .sort() method | ||
| **************************************************************/ | ||
| function sortStudentsAlphabetically(students) { | ||
| //TODO: Add your code here | ||
| } | ||
| // function sortStudentsAlphabetically(students) { | ||
| // const sortedlist =students.sort() | ||
| // return sortedlist | ||
| // } | ||
| // console.log( | ||
| // sortStudentsAlphabetically([ | ||
| // "Eve", | ||
|
|
@@ -77,9 +83,20 @@ function sortStudentsAlphabetically(students) { | |
|
|
||
| Hint: Use the .forEach() and .push() methods | ||
| **************************************************************/ | ||
| function separateOddEven(numbers) { | ||
| //TODO: Add your code here | ||
| } | ||
| // function separateOddEven(numbers) { | ||
| // let odds =[] | ||
| // let evens =[] | ||
| // numbers.forEach((value,index) => { | ||
| // 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])); | ||
|
|
||
| /************************************************************** | ||
|
|
@@ -99,9 +116,12 @@ function separateOddEven(numbers) { | |
|
|
||
| Hint: Use the .filter and .startsWith method | ||
| **************************************************************/ | ||
| function removeItem(code, items) { | ||
| //TODO: Add your code here | ||
| } | ||
| // function removeItem(code, items) { | ||
| // const itemsAfterRemove = items.filter((value,index)=>{ | ||
| // return value.code != code; | ||
| // }) | ||
| // console.log(itemsAfterRemove) | ||
| // } | ||
| // console.log( | ||
| // removeItem("#153", [ | ||
| // { code: "#153", name: "Ball" }, | ||
|
|
@@ -157,15 +177,23 @@ Task 7: | |
| Hint: Use the .map method and separator operator | ||
| **************************************************************/ | ||
| function updateGrades(curve, students) { | ||
| //TODO: Add your code here | ||
|
|
||
| const updatedGrades = students.map((std)=>{ | ||
| if (std.type !=="nerd"){ | ||
| return { ...std, grade:std.grade+curve}; | ||
| } | ||
| else{return std} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "nerds" grade should also be changed, their grades should be subtracted with the curve. |
||
| } | ||
| ) | ||
| return updatedGrades | ||
| } | ||
| // 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" }, | ||
| // ]) | ||
| // ); | ||
| 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" }, | ||
| ]) | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,70 @@ | ||
| const reviewers = require("./reviewers.json"); | ||
| console.log("🚀 ~ file: reviewers.js:2 ~ reviewers:", reviewers); | ||
| import reviewers from './reviewers.json' assert {type: 'json'}; | ||
| // console.log("🚀 ~ file: reviewers.js:2 ~ reviewers:", reviewers); | ||
|
|
||
| /*********************************************************************** | ||
| - This function receives a reviewer object and should return the name of the reviewer. | ||
| ***********************************************************************/ | ||
| function getReviewerName(reviewer) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| } | ||
| // console.log(getReviewerName(reviewers[0])); | ||
| // /*********************************************************************** | ||
| // - This function receives a reviewer object and should return the name of the reviewer. | ||
| // ***********************************************************************/ | ||
| // function getReviewerName(reviewer) { | ||
| // return reviewer.reviewerName | ||
| // } | ||
| // console.log(getReviewerName(reviewers[1])); | ||
|
|
||
| /*********************************************************************** | ||
| - Receives a reviewer object and returns the number of reviews that reviewer has done. | ||
| ************************************************************************/ | ||
| function numberOfReviews(reviewer) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| } | ||
| // console.log(numberOfReviews(reviewers[0])); | ||
| // /*********************************************************************** | ||
| // - Receives a reviewer object and returns the number of reviews that reviewer has done. | ||
| // ************************************************************************/ | ||
| // function numberOfReviews(reviewer) { | ||
| // return reviewer.books.length | ||
|
|
||
| /*********************************************************************** | ||
| - Receives a review title (string) and a reviewer object, | ||
| and returns true if the reviewer object has a review that matches the given review title. | ||
| Otherwise, it returns false. | ||
| - Bonus: uses iteration method .some() | ||
| ***********************************************************************/ | ||
| function reviewerHasReview(reviewTitle, reviewer) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| } | ||
| // } | ||
| // console.log(numberOfReviews(reviewers[1])); | ||
|
|
||
| // /*********************************************************************** | ||
| // - Receives a review title (string) and a reviewer object, | ||
| // and returns true if the reviewer object has a review that matches the given review title. | ||
| // Otherwise, it returns false. | ||
| // - Bonus: uses iteration method .some() | ||
| // ***********************************************************************/ | ||
| // function reviewerHasReview(reviewTitle, reviewer) { | ||
| // return reviewer.books.some(( value)=>{ | ||
| // return value.title === reviewTitle | ||
| // }) | ||
| // } | ||
| // console.log(reviewerHasReview("Becoming", reviewers[0])); | ||
|
|
||
| /************************************************************** | ||
| - Receives a reviewer name (string) and an array of reviewer objects, | ||
| and returns the reviewer object with the same name as the reviewerName provided. | ||
| - Bonus: uses iteration method .find() | ||
| ****************************************************************/ | ||
| function getReviewerByName(reviewerName, reviewers) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| } | ||
| // /************************************************************** | ||
| // - Receives a reviewer name (string) and an array of reviewer objects, | ||
| // and returns the reviewer object with the same name as the reviewerName provided. | ||
| // - Bonus: uses iteration method .find() | ||
| // ****************************************************************/ | ||
| // function getReviewerByName(reviewerName, reviewers) { | ||
| // return reviewers.find((value)=>{ | ||
| // return value.reviewerName ===reviewerName | ||
| // }) | ||
| // } | ||
| // console.log(getReviewerByName("Michelle Obama", reviewers)); | ||
|
|
||
| /************************************************************** | ||
| - Receives a review title (string) and an array of reviewer objects, | ||
| and returns the reviewer object that has done a review with the review title provided. | ||
| - Bonus: uses iteration methods .find() and .some() | ||
| ****************************************************************/ | ||
| // /************************************************************** | ||
| // - Receives a review title (string) and an array of reviewer objects, | ||
| // and returns the reviewer object that has done a review with the review title provided. | ||
| // - Bonus: uses iteration methods .find() and .some() | ||
| // // ****************************************************************/ | ||
| function getReviewerByReviewTitle(reviewTitle, reviewers) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| return reviewers.find((value)=>{ | ||
| return value.books.some(( title)=>{ | ||
| return title.title ===reviewTitle | ||
| }) | ||
| }) | ||
| } | ||
| // console.log(getReviewerByReviewTitle("The Overstory", reviewers)); | ||
| console.log(getReviewerByReviewTitle("The Overstory", reviewers)); | ||
|
|
||
| /************************************************************** | ||
| - Receives a query (string) and an array of reviewer objects, | ||
| and returns an array of the reviewer objects that contain the query in their name/description. | ||
| - Hint: uses string method .includes() and iteration method .filter() | ||
| ****************************************************************/ | ||
| // /************************************************************** | ||
| // - Receives a query (string) and an array of reviewer objects, | ||
| // and returns an array of the reviewer objects that contain the query in their name/description. | ||
| // - Hint: uses string method .includes() and iteration method .filter() | ||
| // ****************************************************************/ | ||
| function searchReviewers(query, reviewers) { | ||
| //TODO: ADD YOUR CODE HERE | ||
| return reviewers.filter((reviewers)=>{ | ||
| return reviewers.reviewerName.includes(query) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you're supposed to check if the query has anything to do with the Also adding |
||
| }) | ||
| } | ||
| // console.log(searchReviewers("o", reviewers)); | ||
| console.log(searchReviewers("o", reviewers)); | ||
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.
Here, you could've added
.toLowerCase()to the value to compare the letter and the string without any issues, instead of changing the letter below fromhtoH.