From 520a3501dc0a4bf1e5025d09e3c0e3630b9348dc Mon Sep 17 00:00:00 2001 From: walidemad Date: Wed, 19 Jul 2023 14:55:06 +0300 Subject: [PATCH 1/2] solve the array task --- arrays.js | 116 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/arrays.js b/arrays.js index 3811265..a394439 100644 --- a/arrays.js +++ b/arrays.js @@ -4,20 +4,37 @@ **************************************************************/ function sumArray(numbers) { //TODO: Add your code here + + + const sum = numbers.reduce((accumulator, currentValue) => + accumulator + currentValue, 0); + return sum; } -// console.log(sumArray([4, 3, 2, 5, -10])); + + +const numbersArray = [1,2,3,4,5]; +const result = sumArray(numbersArray); +console.log(result); + + /************************************************************** 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. **************************************************************/ -function findFirstStringStartingWithLetter(letter, strings) { +function findFirstStringStartingWithLetter(letter, strings) { //TODO: Add your code here + const foundString = strings.find((currentString) => + currentString.startsWith(letter) +); + +return foundString || null } -// console.log( -// findFirstStringStartingWithLetter("h", ["Memory", "Hello", "Happy"]) -// ); +console.log( + findFirstStringStartingWithLetter("H", ["Memory", "Hello", "Happy"]) + +); /************************************************************** Task 3: @@ -26,16 +43,18 @@ function findFirstStringStartingWithLetter(letter, strings) { **************************************************************/ function isPresentIncluded(presentName, presents) { //TODO: Add your code here + return presents.includes(presentName); } -// 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: @@ -44,21 +63,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: @@ -70,14 +91,24 @@ function sortStudentsAlphabetically(students) { **************************************************************/ function separateOddEven(numbers) { //TODO: Add your code here + const odds = []; + const evens = []; + numbers.forEach((number) => { + if (number % 2 === 0) { + evens.push(number); + } else { + odds.push(number); + } + }); + return [odds, evens]; } -// 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: 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 + -Hint: Use the filter method. + - e.g const items = [ { code: '#153', name: 'Ball' }, { code: '#147', name: 'Scissors' }, @@ -90,15 +121,22 @@ function separateOddEven(numbers) { **************************************************************/ function removeItem(code, items) { //TODO: Add your code here + return items.filter((item) => 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" }, + ]) + + ); + const updatedItems = removeItem('#153', items); + + console.log(updatedItems); + + /************************************************************** Task 7: From 0fa8f17008f0a6c0acd19baf98ee1b23546824d4 Mon Sep 17 00:00:00 2001 From: walidemad Date: Fri, 4 Aug 2023 17:35:55 +0300 Subject: [PATCH 2/2] JS Arrays Task --- arrays.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/arrays.js b/arrays.js index a394439..c1c450c 100644 --- a/arrays.js +++ b/arrays.js @@ -63,7 +63,7 @@ console.log( **************************************************************/ function sortStudentsAlphabetically(students) { //TODO: Add your code here - return students.sort(); + return students.sort().toString() } console.log( @@ -132,9 +132,6 @@ console.log( ]) ); - const updatedItems = removeItem('#153', items); - - console.log(updatedItems); @@ -183,14 +180,18 @@ Task 7: **************************************************************/ function updateGrades(curve, students) { //TODO: Add your code here + return students.map((student) => + students.type === "nerd" + ? {...student, grade : student.grade - curve} + :{...student, grade : student.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" }, -// ]) -// ); +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" }, + ]) +);