diff --git a/arrays-basics.js b/arrays-basics.js index 0de468d..1508496 100644 --- a/arrays-basics.js +++ b/arrays-basics.js @@ -2,15 +2,22 @@ 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((v, i) => { +// console.log(v); +// }); +// } // 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"]); @@ -18,30 +25,43 @@ function logArrayWithIndex(arr) {} 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) {} - +// function logEvenNumbers(arr) { +// let evenNumb = arr.every((value, index) => { +// return value % 2; +// }); +// console.log(evenNumb); +// } // 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) { +// return arr.reverse(); +// } -// logArrayBackwards(["one", "two", "three", "four"]); +// 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) {} +// function logLastItem(arr) { +// let last = arr[arr.length - 1]; +// console.log(last); +// } // 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 (i = 0; i < arr.length; i = i + chunkSize) { + let sum = arr.slice(i, i + chunkSize); + // return chunkSize; + console.log(sum); + } +} -// logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); +logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); diff --git a/arrays-intermediate.js b/arrays-intermediate.js index 18b8fb0..5974f3f 100644 --- a/arrays-intermediate.js +++ b/arrays-intermediate.js @@ -5,8 +5,14 @@ **************************************************************/ function sumArray(numbers) { //TODO: Add your code here + let sumArr = numbers.reduce((acc, curr) => { + return acc + curr; + }); + return sumArr; + // why would anyine use consol.log(sumarr) it to look for an undefoed value? } -// console.log(sumArray([4, 3, 2, 5, -10])); + +console.log(sumArray([4, 3, 2, 5, -10])); /************************************************************** Task 2: @@ -17,10 +23,14 @@ function sumArray(numbers) { **************************************************************/ function findFirstStringStartingWithLetter(letter, strings) { //TODO: Add your code here + let findFirst = strings.find((value, index) => { + return value.startsWith(letter.toUpperCase()); + }); + return findFirst; } -// console.log( -// findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"]) -// ); +console.log( + findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"]) +); /************************************************************** Task 3: @@ -31,16 +41,20 @@ function findFirstStringStartingWithLetter(letter, strings) { **************************************************************/ function isPresentIncluded(presentName, presents) { //TODO: Add your code here + let present = presents.map((value, index) => { + return value.toLowerCase().includes(presentName); + }); + return present; } -// console.log( -// isPresentIncluded("puzzle", [ -// "Sparkling Surprise", -// "Enchanted Elegance", -// "Whimsical Wonder", -// "Joyful Jingle", -// "Puzzle", -// ]) -// ); +console.log( + isPresentIncluded("puzzle", [ + "Sparkling Surprise", + "Enchanted Elegance", + "Whimsical Wonder", + "Joyful Jingle", + "Puzzle", + ]) +); /************************************************************** Task 4: @@ -51,21 +65,23 @@ function isPresentIncluded(presentName, presents) { **************************************************************/ 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", -// ]) -// ); +console.log( + sortStudentsAlphabetically([ + "Eve", + "Jasmia", + "Husnia", + "Grace", + "Bob", + "Charlie", + "Alice", + "Dave", + "Um abbas", + "Frank", + ]) +); /************************************************************** Task 5: @@ -79,11 +95,25 @@ function sortStudentsAlphabetically(students) { **************************************************************/ function separateOddEven(numbers) { //TODO: Add your code here + numbers.forEach((value) => { + let odd = []; + let even = []; + if (value % 2 === 0) { + odd.pop(value); + even.push(value); + console.log(`the arr is even ${even}`); + } else { + odd.push(value); + even.pop(value); + console.log(`the arr is odd ${odd}`); + } + }); + return numbers; } -// console.log(separateOddEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); +console.log(separateOddEven([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); /************************************************************** - Task 6: + 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 @@ -101,15 +131,18 @@ function separateOddEven(numbers) { **************************************************************/ function removeItem(code, items) { //TODO: Add your code here + return items.filter((item) => { + return item.code !== code; + }); } -// console.log( -// removeItem("#153", [ -// { code: "#153", name: "Ball" }, -// { code: "#147", name: "Scissors" }, -// { code: "#249", name: "Pillow" }, -// { code: "#149", name: "Tissue" }, -// ]) -// ); +console.log( + removeItem("#153", [ + { code: "#153", name: "Ball" }, + { code: "#147", name: "Scissors" }, + { code: "#249", name: "Pillow" }, + { code: "#149", name: "Tissue" }, + ]) +); /************************************************************** Task 7: @@ -158,14 +191,24 @@ Task 7: **************************************************************/ function updateGrades(curve, students) { //TODO: Add your code here + let updatedStudents = [...students]; + updatedStudents.map((student) => { + if (student.type === "nerd") { + student.grade - curve; + } else { + student.grade + curve; + } + return student; + }); + return updatedStudents; } -// 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" }, + ]) +); diff --git a/arrays.js b/arrays.js new file mode 100644 index 0000000..e69de29 diff --git a/index.html b/index.html index d971aeb..1179481 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,13 @@ - - - - + + + + Arrays - - - + + diff --git a/reviewers.js b/reviewers.js index eda2973..68835e2 100644 --- a/reviewers.js +++ b/reviewers.js @@ -6,8 +6,9 @@ console.log("🚀 ~ file: reviewers.js:2 ~ reviewers:", reviewers); ***********************************************************************/ function getReviewerName(reviewer) { //TODO: ADD YOUR CODE HERE + return reviewer.reviewerName; } -// console.log(getReviewerName(reviewers[0])); +console.log(getReviewerName(reviewers[0])); /*********************************************************************** - Receives a reviewer object and returns the number of reviews that reviewer has done.