Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions arrays-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,64 @@
Task 8 (logArray):
Create a function logArray that receives an array as a parameter and logs each item in the array.
**************************************************************/
function logArray(arr) {}

// logArray([1, 2, 3, 4, 5]);
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) {}
function logArrayWithIndex(arr) {
arr.forEach((value,index)=>{console.log(value,index)})
}

// logArrayWithIndex(["apple", "banana", "orange"]);
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) {}

// logEvenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
function logEvenNumbers(arr) {
for(let i=0;i<arr.length; i++){
if(arr[i]%2 ==0){
console.log(arr[i] )
}
}
}
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) {}
function logArrayBackwards(arr) {
console.log(arr.reverse())
}

// logArrayBackwards(["one", "two", "three", "four"]);
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) {}
function logLastItem(arr) {
console.log(arr[arr.length - 1])
}

// logLastItem(["a", "b", "c", "d"]);
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) {}
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);
logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3);
90 changes: 59 additions & 31 deletions arrays-intermediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]));

/**************************************************************
Expand All @@ -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);})

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 from h to H.

// return result
// }
// console.log(
// findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"])
// findFirstStringStartingWithLetter("H", ["Memory", "Hello", "Happy"])
// );

/**************************************************************
Expand All @@ -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)
Copy link

@AlhassanAli01 AlhassanAli01 Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you're supposed to use the map method to make all the names of the presents lower cased, and then use the includes on the lower cased present.

// }
// console.log(
// isPresentIncluded("puzzle", [
// isPresentIncluded("blabla", [
// "Sparkling Surprise",
// "Enchanted Elegance",
// "Whimsical Wonder",
Expand All @@ -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",
Expand All @@ -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]));

/**************************************************************
Expand All @@ -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" },
Expand Down Expand Up @@ -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}

Choose a reason for hiding this comment

The 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" },
])
);
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>Arrays</title>
</head>
<body>
<script src="./arrays.js" type="module" />
<script src="./arrays-basics.js" type="module" />

</body>
</html>
105 changes: 58 additions & 47 deletions reviewers.js
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)
Copy link

@AlhassanAli01 AlhassanAli01 Feb 22, 2024

Choose a reason for hiding this comment

The 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 reviewerName and description, therefore adding an or operator to include the description is the way to go.

Also adding .toLowercase method to the strings is the best in this scenario.

 return reviewers.filter((reviewer) => {
  let reviewerName = reviewer.reviewerName.toLowerCase();
  let description = reviewer.description.toLowerCase();
  return reviewerName.includes(query) || description.includes(query);
});

})
}
// console.log(searchReviewers("o", reviewers));
console.log(searchReviewers("o", reviewers));