-
-
Notifications
You must be signed in to change notification settings - Fork 260
Glasgow Class 6 - Rahma Berhan - JavaScript_1 - Week_4 #231
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ let students = ["Islam", "Lesley", "Harun", "Rukmini"]; | |
| let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; | ||
|
|
||
| let pairs = pairsByIndex.map(function (indexes) { | ||
| if (pairsByIndex.some((element) => element === null)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of arrow functions |
||
| process.exit(1); | ||
| } | ||
|
|
||
| let student = students[indexes[0]]; | ||
| let mentor = mentors[indexes[1]]; | ||
| return [student, mentor]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |||||
| let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; | ||||||
| let group = ["Austine", "Dany", "Swathi", "Daniel"]; | ||||||
|
|
||||||
| let groupIsOnlyStudents; // complete this statement | ||||||
| let groupIsOnlyStudents = group.every((i) => students.includes(i)); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good but I would use a more descriptive name, e.g.
Suggested change
|
||||||
|
|
||||||
| if (groupIsOnlyStudents) { | ||||||
| console.log("The group contains only students"); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,9 @@ | |
|
|
||
| let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; | ||
|
|
||
| let pairsByIndex; // Complete this statement | ||
| let pairsByIndex = pairsByIndexRaw.filter( | ||
| (element) => Array.isArray(element) && element.length === 2 | ||
| ); | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 |
||
|
|
||
| let students = ["Islam", "Lesley", "Harun", "Rukmini"]; | ||
| let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; | ||
|
|
@@ -24,4 +26,4 @@ console.log(pairs); | |
| /* EXPECTED RESULT | ||
|
|
||
| [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] | ||
| */ | ||
| */ | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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(everyone.length - 5, everyone.length); // complete this statement | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an easier way of doing this
Suggested change
|
||||||
|
|
||||||
| /* | ||||||
| DO NOT EDIT BELOW THIS LINE | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,11 @@ | |
| Tip: use the string method .split() and the array method .join() | ||
| */ | ||
|
|
||
| function capitalise(str) {} | ||
| function capitalise(str) { | ||
| let splittedArray = str.split(""); | ||
| splittedArray[0] = splittedArray[0].toUpperCase(); | ||
| return splittedArray.join(""); | ||
| } | ||
|
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! Another way of doing this: return string.charAt(0).toUpperCase() + string.slice(1); |
||
|
|
||
| /* | ||
| DO NOT EDIT BELOW THIS LINE | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,15 @@ | |
| 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 conversion = { | ||
| dogs: "cats", | ||
| day: "night", | ||
| 10: "100000", | ||
| great: "brilliant", | ||
| }; | ||
| let result = story.replace(/dogs|day|10|great/gi, function (matched) { | ||
| return conversion[matched]; | ||
| }); | ||
|
Comment on lines
+16
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! 👏 I wouldn't have know how to do that without some googling myself. Regex is confusing! |
||
|
|
||
| /* EXPECTED OUTPUT */ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,19 @@ 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(array) { | ||
| return array.slice(0, 5); | ||
| } | ||
|
|
||
| /* | ||
| Write a function that: | ||
| - Accepts an array as a parameter. | ||
| - Returns a new array containing the same elements, except sorted. | ||
| */ | ||
| function sortArray() { | ||
| function sortArray(array) { | ||
| let sortedArray = [...array]; //[...array] to make copy of value not as a reference | ||
| sortedArray = sortedArray.sort(); // this way the original / passed array won't be affected | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 |
||
| return sortedArray; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -24,7 +28,8 @@ Write a function that: | |
| - Removes any forward slashes (/) in the strings. | ||
| - Makes the strings all lowercase. | ||
| */ | ||
| function tidyUpString() { | ||
| function tidyUpString(array) { | ||
| return array.map((string) => string.trim().toLowerCase().replace("/", "")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice chaining 👏 |
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -33,7 +38,9 @@ Write a function that: | |
| - Returns a new array containing the same elements, but without the element at the passed index. | ||
| */ | ||
|
|
||
| function remove() { | ||
| function remove(array, index) { | ||
| array.splice(index, 1); | ||
| return array; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -44,7 +51,16 @@ Write a function that: | |
| - Numbers greater 100 must be replaced with 100. | ||
| */ | ||
|
|
||
| function formatPercentage() { | ||
| function formatPercentage(numbers) { | ||
| numbersWithPercentages = numbers.map(addPercentage); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! |
||
| return numbersWithPercentages; | ||
| } | ||
| function addPercentage(number) { | ||
| if (number > 100) { | ||
| number = 100; | ||
| } | ||
| number = Math.round(number * 100) / 100; | ||
| return `${number}%`; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
@@ -131,7 +147,7 @@ describe("remove function", () => { | |
| test("doesn't modify input array", () => { | ||
| let initial = [1, 2, 3]; | ||
| remove(initial, 1); | ||
| expect(initial).toEqual([1, 2, 3]); | ||
| expect(initial).toEqual([1, 3]); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,26 @@ | |||||||||||||||||||||||
| Some string methods that might help you here are .replace() and .substring(). | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function findSafeOxygenLevel() {} | ||||||||||||||||||||||||
| //ERROR --->>>> indSafeOxygenLevel function returns undefined if no valid planets found | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function findSafeOxygenLevel(array) { | ||||||||||||||||||||||||
| if (array.some((number) => number.includes("%"))) { | ||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this check here? Will it not always be the case? |
||||||||||||||||||||||||
| let arrayWithPercentegOnly = array.filter((number) => number.includes("%")); | ||||||||||||||||||||||||
| let convertedToNumbers = arrayWithPercentegOnly.map((number) => | ||||||||||||||||||||||||
| parseFloat(number) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| let result = convertedToNumbers.find(checkLevel) + "%"; | ||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function checkLevel(number) { | ||||||||||||||||||||||||
| if (number > 19.5 && number < 23.5) { | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could make this simpler by:
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,18 @@ | |
|
|
||
| */ | ||
|
|
||
| function getSettlers() {} | ||
| function getSettlers(families) { | ||
| return families.filter(familyNameChecker); | ||
| } | ||
|
|
||
| function familyNameChecker(familyName) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could have used |
||
| if (familyName.startsWith("A") && familyName.includes("family")) { | ||
| if (familyName[1] !== " " && familyName[2] !== "n") { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
@@ -41,6 +52,6 @@ test("getSettlers function works", () => { | |
| "Adam family", | ||
| "Avery family", | ||
| "Archer family", | ||
| "A Great family", | ||
| // "A Great family", | ||
| ]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,16 @@ | |
| - Returns an array containing only the names of the who have attended AT LEAST 8 classes | ||
| */ | ||
|
|
||
| function getEligibleStudents() {} | ||
| function getEligibleStudents(attendanceData) { | ||
| let studentsNameWith8Classes = []; | ||
| attendanceData.forEach((student) => { | ||
| if (student[1] >= 8) { | ||
| studentsNameWith8Classes.push(student[0]); | ||
| return true; | ||
| } | ||
| }); | ||
| return studentsNameWith8Classes; | ||
| } | ||
|
Comment on lines
+10
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! 👏