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
42 changes: 30 additions & 12 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) {}
// 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"]);

/**************************************************************
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) {
// return arr.filter((items, index) => {
// return items % 2 === 0;
// });
// }
// console.log(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) {}

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

// logLastItem(["a", "b", "c", "d"]);
// function logLastItem(arr) {
// return arr[arr.length - 1];
// }
// console.log(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 = i + chunkSize) {
// const checks = arr.slice(i, i + chunkSize);
// console.log(checks);
// }
// }

// logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3);
68 changes: 49 additions & 19 deletions arrays-intermediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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) {
// //TODO: Add your code here
// return numbers.reduce((items, value)=> items+value )
// }
// console.log(sumArray([4, 3, 2, 5, -10]));

/**************************************************************
Task 2:
Create a function findFirstStringStartingWithA that receives an array of strings as a parameter
Expand All @@ -17,6 +17,7 @@ function sumArray(numbers) {
**************************************************************/
function findFirstStringStartingWithLetter(letter, strings) {
//TODO: Add your code here
return strings.find((str) => str.toLowerCase().startswith(letter));
}
// console.log(
// findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"])
Expand All @@ -29,9 +30,12 @@ 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.map((items, index) =>
// items.toLowerCase().includes(presentName)
// );
// //TODO: Add your code here
// }
// console.log(
// isPresentIncluded("puzzle", [
// "Sparkling Surprise",
Expand All @@ -46,12 +50,14 @@ function isPresentIncluded(presentName, presents) {
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
}
// function sortStudentsAlphabetically(students) {
// return students.sort();
// //TODO: Add your code here
// }
// console.log(
// sortStudentsAlphabetically([
// "Eve",
Expand All @@ -77,9 +83,21 @@ function sortStudentsAlphabetically(students) {

Hint: Use the .forEach() and .push() methods
**************************************************************/
function separateOddEven(numbers) {
//TODO: Add your code here
}
// function separateOddEven(numbers) {
// const odds = [];
// const even = [];
// numbers.forEach((items) => {
// if (items % 2 === 0) {
// odds.push(items);
// } else {
// even.push(items);
// }

// });
// return{even,odds}
// }
// //TODO: Add your code here

// console.log(separateOddEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

/**************************************************************
Expand All @@ -99,9 +117,13 @@ function separateOddEven(numbers) {

Hint: Use the .filter and .startsWith method
**************************************************************/
function removeItem(code, items) {
//TODO: Add your code here
}
// function removeItem(code, items) {
// return items.filter((value,index)=>
// value.code != code
// )
// }
// //TODO: Add your code here

// console.log(
// removeItem("#153", [
// { code: "#153", name: "Ball" },
Expand Down Expand Up @@ -156,9 +178,17 @@ Task 7:

Hint: Use the .map method and separator operator
**************************************************************/
function updateGrades(curve, students) {
//TODO: Add your code here
}
// function updateGrades(curve, students) {
// students.map((items, index) => {
// if (items.type === "regular") {
// return items.grade += curve
// } else {
// return items.grade -= curve
// }
// });
// return students;
// }
// //TODO: Add your code here
// console.log(
// updateGrades(10, [
// { firstName: "Jaber", lastName: "jabarbar", grade: 10, type: "regular" },
Expand Down
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<title>Arrays</title>
</head>
<body>
<script src="./arrays.js" type="module" />
</body>
<!-- <script src="./arrays-basics.js"></script></body> -->
<!-- <script src="./arrays-intermediate.js"></script> -->
<script src="reviewers.js"></script>

</html>
29 changes: 26 additions & 3 deletions reviewers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const reviewers = require("./reviewers.json");
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
}
// function getReviewerName(reviewer) {
//TODO: ADD YOUR CODE HERE
// console.log(reviewer.reviewerName);
// }
// console.log(getReviewerName(reviewers[0]));

/***********************************************************************
- Receives a reviewer object and returns the number of reviews that reviewer has done.
************************************************************************/
function numberOfReviews(reviewer) {
//TODO: ADD YOUR CODE HERE
return reviewer.books.length;
}
// console.log(numberOfReviews(reviewers[0]));

Expand All @@ -25,6 +28,9 @@ function numberOfReviews(reviewer) {
***********************************************************************/
function reviewerHasReview(reviewTitle, reviewer) {
//TODO: ADD YOUR CODE HERE
reviewer.books.some((items) => {
return items.title === reviewTitle;
});
}
// console.log(reviewerHasReview("Becoming", reviewers[0]));

Expand All @@ -35,6 +41,7 @@ function reviewerHasReview(reviewTitle, reviewer) {
****************************************************************/
function getReviewerByName(reviewerName, reviewers) {
//TODO: ADD YOUR CODE HERE
reviewers.find((items) => items.reviewerName === reviewerName);
}
// console.log(getReviewerByName("Michelle Obama", reviewers));

Expand All @@ -45,6 +52,11 @@ function getReviewerByName(reviewerName, reviewers) {
****************************************************************/
function getReviewerByReviewTitle(reviewTitle, reviewers) {
//TODO: ADD YOUR CODE HERE
return reviewers.find((items) => {
items.some((value) => {
return value.books === reviewTitle;

Choose a reason for hiding this comment

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

the condition for getting the reviewer by the title is incorrect, you should do this instead:

return reviewers.find((items) => {
  items.some((value) => {
     return value.title === reviewTitle;
   } 
}

Which is basically using the reviewerHasReview function, so you can use that instead:

  return reviewers.find((reviewer) => reviewerHasReview(reviewTitle, reviewer));

});
});
}
// console.log(getReviewerByReviewTitle("The Overstory", reviewers));

Expand All @@ -56,4 +68,15 @@ function getReviewerByReviewTitle(reviewTitle, reviewers) {
function searchReviewers(query, reviewers) {
Copy link

@AlhassanAli01 AlhassanAli01 Feb 18, 2024

Choose a reason for hiding this comment

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

You can not use return three times in the same function, the return should be only used once, unless it's inside an if statement or a swtich case.

This function could be easily done using the filter method:

 return reviewers.filter(
    (reviewer) =>
      reviewer.reviewerName.includes(query) ||
      reviewer.description.includes(query)
  );

//TODO: ADD YOUR CODE HERE
}
return reviewers.map((items) => {
items.reviewerName = items.reviewerName.toWloerCase();
});
return reviewers.map((items) => {
items.description = items.description.toWloerCase();
});
return reviewers.fiter((items) => {
if (items.reviewerName.includes(query) && items.description.includes(query)) {
return items;
}
});
// console.log(searchReviewers("o", reviewers));