From 20460c86a480e8ba03832f7ed14aa860a708848d Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 5 Apr 2023 22:56:55 +0100 Subject: [PATCH 1/3] Completed the exercises section --- 1-exercises/A-array-find/exercise.js | 40 +++++++++++++++ .../A-array-find/practicegroupstudy.js | 51 +++++++++++++++++++ 1-exercises/B-array-some/exercise.js | 6 ++- 1-exercises/B-array-some/grouppractice.js | 15 ++++++ 1-exercises/C-array-every/exercise.js | 7 ++- 1-exercises/D-array-filter/exercise.js | 2 +- 1-exercises/E-array-map/exercise.js | 2 +- 1-exercises/F-array-forEach/README.md | 3 +- 1-exercises/F-array-forEach/exercise.js | 13 +++++ 1-exercises/G-array-methods/exercise.js | 2 +- 1-exercises/G-array-methods/exercise2.js | 4 +- 1-exercises/H-array-methods-2/exercise.js | 4 +- 1-exercises/H-array-methods-2/exercise2.js | 23 ++++++++- 1-exercises/H-array-methods-2/exercise3.js | 2 +- 1-exercises/I-string-replace/exercise.js | 3 +- 1-exercises/J-string-substring/exercise.js | 2 +- 1-exercises/J-string-substring/exercise2.js | 10 ++-- 1-exercises/J-string-substring/exercise3.js | 2 +- 2-mandatory/1-create-functions.js | 12 +++-- 2-mandatory/5-eligible-students.js | 9 +++- 2-mandatory/8-password-validator.js | 15 +++++- 21 files changed, 203 insertions(+), 24 deletions(-) create mode 100644 1-exercises/A-array-find/practicegroupstudy.js create mode 100644 1-exercises/B-array-some/grouppractice.js diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..c7ce60ff 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -3,8 +3,11 @@ Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters. */ +// we are using function with a parameter that is an array of string + // write your code here + let names = [ "Rakesh", "Antonio", @@ -17,9 +20,46 @@ let names = [ "Ahmed", ]; +function isLongName(singleName) { + return singleName.length > 7; +} + +let longName = names.find(isLongName); + +function isStartingWithA(singleName) { + return singleName[0] === "A"; +} + +let nameStartingWithA = names.find(isStartingWithA); + +function findLongNameThatStartsWithA(names) { + return nameStartingWithA && longName; + +} + let longNameThatStartsWithA = findLongNameThatStartsWithA(names); + console.log(longNameThatStartsWithA); /* EXPECTED OUTPUT */ // "Alexandra" + + +// function findLongName(names) { +// let longName = []; +// if (names.length > 7) { +// longName.push(names); +// } +// return longName; +// } + +// function findLongNameThatStartsWithA(string){ +// let LongNameStartingWithA = []; +// for (let letter of string) { +// if (findLongName(string) && letter[0] === "A") { +// LongNameStartingWithA.push(string) +// } +// } +// return LongNameStartingWithA +// } \ No newline at end of file diff --git a/1-exercises/A-array-find/practicegroupstudy.js b/1-exercises/A-array-find/practicegroupstudy.js new file mode 100644 index 00000000..9e6c8e7e --- /dev/null +++ b/1-exercises/A-array-find/practicegroupstudy.js @@ -0,0 +1,51 @@ +function hasOver10Alphabets (word) { + if (word.length > 10 ) { + return true; + } else { + return false; + } +} + +function hasOver10Alphabetss (word) { + return word.length > 10 +} + + +/* +(input) => (output) + +input is word +output is word.length > 10 +word => word.length > 10 +(argument) => (what it returns) + +*/ + +function functionName(anythingGoes) { + let isItGreaterThanTen = anythingGoes > 10; + return isItGreaterThanTen; +} + + + +let names = ["Bedi", " Afsoon", "Emilie", "Afsha", "BediAfsooonEmilieAfsha", "loveeveryday"]; + +let longNames = names.find(hasOver10Alphabets); + +let longNames1 = names.find(function hasOver10Alphabets (word) { + if (word.length > 10 ) { + return true; + } else { + return false; + } +}); + +let longNames2 = names.find(word => word.length > 10); + +console.log(longNames) + +let longNameList = names.find(word => word.length > 10); + + +// console.log(hasOver10Alphabets("Emelie")); +// console.log(hasOver10Alphabetss("Emelieeeeee")); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..d4b7984a 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -15,10 +15,14 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; +for (let eachPair of pairsByIndex) { + if (eachPair === null) { + return process.exit(1); + }; +}; let pairs = pairsByIndex.map(function (indexes) { let student = students[indexes[0]]; let mentor = mentors[indexes[1]]; return [student, mentor]; }); - console.log(pairs); diff --git a/1-exercises/B-array-some/grouppractice.js b/1-exercises/B-array-some/grouppractice.js new file mode 100644 index 00000000..877b3668 --- /dev/null +++ b/1-exercises/B-array-some/grouppractice.js @@ -0,0 +1,15 @@ +let group = ["afsha", "emilie", "bedi", "junita"]; + +// name.length > 7 + +function areAllNameShort(names) { + let foundLongName = false; + for (element of group) { + if (element.length>) + } +} + + +function areAllNameShort(names) { + +} \ No newline at end of file diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..d961bf69 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,11 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +let groupIsOnlyStudents = group.every(isOnlyStudent); // complete this statement + +function isOnlyStudent(list) { + return list.includes(students); +} if (groupIsOnlyStudents) { console.log("The group contains only students"); @@ -16,3 +20,4 @@ if (groupIsOnlyStudents) { /* EXPECTED RESULT */ // The group does not contain only students + diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..2dfe1f33 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,7 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +let pairsByIndex = pairsByIndexRaw.filter((pair) => Array.isArray(pair) && pair.length === 2); // Complete this statement let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..b5ba4a35 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,7 +3,7 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +let numbersMultipliedByOneHundred = numbers.map(number => number * 100); // complete this statement console.log(numbersMultipliedByOneHundred); diff --git a/1-exercises/F-array-forEach/README.md b/1-exercises/F-array-forEach/README.md index dfc43ba7..c6469973 100644 --- a/1-exercises/F-array-forEach/README.md +++ b/1-exercises/F-array-forEach/README.md @@ -36,9 +36,10 @@ However, it is good practise to write small functions with a single responsibili ```js function formatName(name) { - return name.split("")[0].toUpperCase() + name.slice(1); + return name.split(" ")[0].toUpperCase() + name.slice(1); } + names.map(formatName).forEach(function(name, index) { console.log(index + ": " + name); }); diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..d6db2e5d 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,19 @@ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach(function replaceWithFizzBuzz(number) { + if (number % 3 === 0 && number % 5 === 0) { + console.log("FizzBuzz"); + } else if (number % 3 === 0) { + console.log("Fizz"); + } else if (number % 5 === 0) { + console.log("Buzz"); + } else console.log(number); + } +); + + + /* EXPECTED OUTPUT */ /* diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..bc504f2a 100644 --- a/1-exercises/G-array-methods/exercise.js +++ b/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ let numbers = [3, 2, 1]; -let sortedNumbers; // complete this statement +let sortedNumbers = numbers.sort(); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js index 4c68c3a6..19a25fd0 100644 --- a/1-exercises/G-array-methods/exercise2.js +++ b/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,9 @@ let mentors = ["Daniel", "Irina", "Rares"]; let students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -let everyone; // complete this statement + +let everyone = mentors.concat(students); // complete this statement +// let everyone = [...mentors, ...students]; // alternate solution /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..331cc4aa 100644 --- a/1-exercises/H-array-methods-2/exercise.js +++ b/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ let everyone = [ "Swathi", ]; -let firstFive; // complete this statement -let lastFive; // complete this statement +let firstFive = everyone.slice(0, 5); // complete this statement +let lastFive = everyone.slice(2, 7); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js index 14bb4318..b8ecb914 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -5,9 +5,30 @@ It should return a string with the first letter in uppercase For example, capitailise("hello") should return "Hello" Tip: use the string method .split() and the array method .join() + + we can split the string into an array + ['d','a','n','i','e','l'] + + what array method can we use to change the first letter to a capital? + + let newArr = arr.map(function (currentValue, index) { + if (index === 0) { + return currentValue.toUpperCase(); + } + return currentValue +}); + */ -function capitalise(str) {} +function capitalise(str) { + let newArray = []; + newArray = str.split(""); + newArray[0] = newArray[0].toUpperCase(); + return newArray.join(""); + // return str.split("")[0].toUpperCase() + str.slice(1); +} + + /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js index c8e079e4..7279417e 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); // complete this statement } /* diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..f3b49257 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,8 @@ let story = "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."; -let result = story.replace("", ""); +let result = + story.replace("dogs", "cats").replace("day", "night").replace("10", "100000").replace("dogs", "cats").replace("great", "brilliant").replace("day", "night"); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..488d1f5d 100644 --- a/1-exercises/J-string-substring/exercise.js +++ b/1-exercises/J-string-substring/exercise.js @@ -6,7 +6,7 @@ let statement = "I like programming and dogs"; -statement = statement.substring(); +statement = statement.substring(0, 19); console.log(statement); diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..cb541c6a 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -14,11 +14,11 @@ let names = [ "Arron Graham", ]; -names[0] = names[0].substring(); -names[1] = names[1].substring(); -names[2] = names[2].substring(); -names[3] = names[3].substring(); -names[4] = names[4].substring(); +names[0] = names[0].substring(0, 6); +names[1] = names[1].substring(0, 7); +names[2] = names[2].substring(0, 4); +names[3] = names[3].substring(0, 4); +names[4] = names[4].substring(0, 5); names.forEach((name) => { console.log(name); diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js index 14f77417..356e4701 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -8,7 +8,7 @@ let statement = "I do not like programming"; -let result = ""; +let result = statement.substring(0, 5) + statement.substring(9, statement.length); console.log(result); diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..4941be25 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,8 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the first five elements of the passed array. */ -function first5() { +function first5(arrayOfItems) { + return arrayOfItems.slice(0, 5); } /* @@ -11,7 +12,8 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { +function sortArray(arrayOfItems) { + return Array.from(arrayOfItems).sort(); } /* @@ -24,7 +26,11 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(arrayOfStrings) { + for (const singleString of arrayOfStrings) { + console.log(singleString.trim()); + } + return singleString.trim().replace("/","").toLowerCase(); } /* diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..d940d0a8 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,14 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function findName(studentArray) { + let filterArray = studentArray.filter((element) => {(element[1] >= 8) { + return element[0]; + } + }); + // let modifyArray = filterArray.map(element => element[0]) + // return modifyArray +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..ba6d50b7 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,20 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + return passwords.map( + (element, i, arr) => + element.length >= 5 && + containsLowercaseLetter(element) && + containsNumber(element) && + containsUppercaseLetter(element) && + containsSymbol(element) && + arr.indexOf(element) === i + ); +} + + + // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { From 7a2cddfc8715cdd4e2b516830c026501d1a66d55 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 5 Apr 2023 23:33:15 +0100 Subject: [PATCH 2/3] Made a few changes to exercise and mandatory sections --- 2-mandatory/1-create-functions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 4941be25..348f2440 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -51,6 +51,7 @@ Write a function that: */ function formatPercentage() { + } /* ======= TESTS - DO NOT MODIFY ===== */ From c7a7135e4d0af57af140ecd0a9d87203c0f43667 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 5 Apr 2023 23:34:35 +0100 Subject: [PATCH 3/3] made some changes to exerise and mandatory parts --- .../A-array-find/practicegroupstudy.js | 51 ------------------- 1-exercises/B-array-some/grouppractice.js | 15 ------ 2-mandatory/7-lane-names.js | 10 +++- 3 files changed, 9 insertions(+), 67 deletions(-) delete mode 100644 1-exercises/A-array-find/practicegroupstudy.js delete mode 100644 1-exercises/B-array-some/grouppractice.js diff --git a/1-exercises/A-array-find/practicegroupstudy.js b/1-exercises/A-array-find/practicegroupstudy.js deleted file mode 100644 index 9e6c8e7e..00000000 --- a/1-exercises/A-array-find/practicegroupstudy.js +++ /dev/null @@ -1,51 +0,0 @@ -function hasOver10Alphabets (word) { - if (word.length > 10 ) { - return true; - } else { - return false; - } -} - -function hasOver10Alphabetss (word) { - return word.length > 10 -} - - -/* -(input) => (output) - -input is word -output is word.length > 10 -word => word.length > 10 -(argument) => (what it returns) - -*/ - -function functionName(anythingGoes) { - let isItGreaterThanTen = anythingGoes > 10; - return isItGreaterThanTen; -} - - - -let names = ["Bedi", " Afsoon", "Emilie", "Afsha", "BediAfsooonEmilieAfsha", "loveeveryday"]; - -let longNames = names.find(hasOver10Alphabets); - -let longNames1 = names.find(function hasOver10Alphabets (word) { - if (word.length > 10 ) { - return true; - } else { - return false; - } -}); - -let longNames2 = names.find(word => word.length > 10); - -console.log(longNames) - -let longNameList = names.find(word => word.length > 10); - - -// console.log(hasOver10Alphabets("Emelie")); -// console.log(hasOver10Alphabetss("Emelieeeeee")); diff --git a/1-exercises/B-array-some/grouppractice.js b/1-exercises/B-array-some/grouppractice.js deleted file mode 100644 index 877b3668..00000000 --- a/1-exercises/B-array-some/grouppractice.js +++ /dev/null @@ -1,15 +0,0 @@ -let group = ["afsha", "emilie", "bedi", "junita"]; - -// name.length > 7 - -function areAllNameShort(names) { - let foundLongName = false; - for (element of group) { - if (element.length>) - } -} - - -function areAllNameShort(names) { - -} \ No newline at end of file diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..d1fc7edc 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,15 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +let laneNames = []; + + +function getLanes(roadNames) { + if (roadNames.includes("Lane")) { + laneNames.push(roadNames); + } + return laneNames; +} /* ======= TESTS - DO NOT MODIFY ===== */