Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London Class 10 - Andrius Isin - JS1 - Week 4 #232

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
9 changes: 7 additions & 2 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ let names = [
"Karim",
"Ahmed",
];

let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
function getlongNameThatStartsWithA(name) {
return name > 7 && name.startsWith("A");
}
let longNameThatStartsWithA = names.find(getlongNameThatStartsWithA);
// let longNameThatStartsWithA = names.find((element) => {
// return element.length > 7 && element.startsWith("A");
// });

Copy link

Choose a reason for hiding this comment

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

I really like the "startsWith" method you used. Nice! but can you think of another way of checking the second condition without using any method?

console.log(longNameThatStartsWithA);

Expand Down
10 changes: 7 additions & 3 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

let pairs = pairsByIndex.map(function (indexes) {
let student = students[indexes[0]];
let mentor = mentors[indexes[1]];
return [student, mentor];
if (indexes !== null) {
Copy link

Choose a reason for hiding this comment

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

Your condition works fine, so well done for that! but the aim of this exercise is using some() method on pairsByIndex array. Could you think of using some() instead of line 19.

let student = students[indexes[0]];
let mentor = mentors[indexes[1]];
return [student, mentor];
} else {
process.exit([1]);
}
});

console.log(pairs);
4 changes: 2 additions & 2 deletions 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/

let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];
let group = ["Austine", "Dany", "Swathi"];

let groupIsOnlyStudents; // complete this statement
let groupIsOnlyStudents = group.every((element) => students.includes(element));

if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand Down
6 changes: 4 additions & 2 deletions 1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
return Array.isArray(element) && element.length === 2;
});

let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand All @@ -24,4 +26,4 @@ console.log(pairs);
/* EXPECTED RESULT

[ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ]
*/
*/
4 changes: 2 additions & 2 deletions 1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

let numbers = [0.1, 0.2, 0.3, 0.4, 0.5];

let numbersMultipliedByOneHundred; // complete this statement
let numbersMultipliedByOneHundred = numbers.map((num) => num * 100);

console.log(numbersMultipliedByOneHundred);

/* EXPECTED RESULT

[10, 20, 30, 40, 50]
*/
*/
10 changes: 9 additions & 1 deletion 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
*/

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

let newArray = arr.forEach((number) => {
if (number % 3 === 0) {
console.log("Fizz");
} else if (number % 5 === 0) {
console.log("Buzz");
} else {
Copy link

@migmow migmow Mar 31, 2023

Choose a reason for hiding this comment

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

Please read the instruction again, Andrius. There are 3 conditions. You have successfully done first and second condetions but could you add the last condition as well and print "FizzBuzz" when needed.
Also, it's a good practice if in the future you could use ternary operator instead of if else chaining.

console.log(number);
}
});
/* EXPECTED OUTPUT */

/*
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/G-array-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

let numbers = [3, 2, 1];
let sortedNumbers; // complete this statement
let sortedNumbers = numbers.sort();

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/G-array-methods/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let mentors = ["Daniel", "Irina", "Rares"];
let students = ["Rukmini", "Abdul", "Austine", "Swathi"];

let everyone; // complete this statement
let everyone = mentors.concat(students);

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/H-array-methods-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ let everyone = [
"Swathi",
];

let firstFive; // complete this statement
let lastFive; // complete this statement
let firstFive = everyone.slice(0, 5);
let lastFive = everyone.slice(-5);

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
6 changes: 5 additions & 1 deletion 1-exercises/H-array-methods-2/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
Tip: use the string method .split() and the array method .join()
*/

function capitalise(str) {}
function capitalise(str) {
let arr = str.split("");
arr[0] = arr[0].toUpperCase();
return arr.join("");
}

Copy link

Choose a reason for hiding this comment

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

Nice one indeed!

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/H-array-methods-2/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"];

function isInUK(country) {
return; // complete this statement
return ukNations.includes(country);
}

/*
Expand Down
7 changes: 6 additions & 1 deletion 1-exercises/I-string-replace/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
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/g, "cats")
.replace(/day/g, "night")
.replace(/great/g, "brilliant")
.replace(/10/g, "100000");
console.log(result);
Copy link

Choose a reason for hiding this comment

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

Is there any specific reason that you used regular expression?


/* EXPECTED OUTPUT */

Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/J-string-substring/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

let statement = "I like programming and dogs";

statement = statement.substring();
statement = statement.substring(0, 19);

console.log(statement);

Expand Down
10 changes: 5 additions & 5 deletions 1-exercises/J-string-substring/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

let statement = "I do not like programming";

let result = "";
let result = `${statement.substring(0, 4)}${statement.substring(
8,
statement.length
)}`;

console.log(result);

Expand Down
26 changes: 19 additions & 7 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the first five elements of the passed array.
npm test -- --testPathPattern 1-create-functions.js
*/
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) {
return array.slice().sort();
}

/*
NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week.
Think about what is better about this solution than your one last week, and what is worse.
NOTE: This exercise is the same as one you did last week - try to do it again using things
you learnt this week. Think about what is better about this solution than your one last
week, and what is worse.

Write a function that:
- Takes an array of strings as input.
- Removes any spaces in the beginning or end each string.
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(array) {
return array.map((string) => string.trim().replace("/", "").toLowerCase());
}

/*
Expand All @@ -33,7 +38,8 @@ 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) {
return array.filter((element, i) => i !== index);
Copy link

Choose a reason for hiding this comment

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

This could pass your tests but it isn't very efficient. Imagine if you have a lenghthy array, looping through isn't efficient here. I recommend using methods like slice and concat

}

/*
Expand All @@ -44,7 +50,13 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(numbers) {
return numbers.map((number) => {
if (number > 100) {
Copy link

Choose a reason for hiding this comment

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

The instruction is saying if number >= 100 then return "100%". Please amend your if statement

number = 100;
Copy link

Choose a reason for hiding this comment

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

This bit isn't right. Can you think why? You are looping through array of numbers and then reassigning 100 to each element in return.

}
return `${Math.round(number * 100) / 100}%`;
});
Copy link

Choose a reason for hiding this comment

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

This function doesn't pass the test and returns undefined.

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
13 changes: 11 additions & 2 deletions 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}

function findSafeOxygenLevel(oxygen) {
let firstSafePlanet = oxygen
.filter((planet) => planet.includes("%"))
.map((planet) => Number(planet.replace("%", "")))
.find((planet) => planet > 19.5 && planet < 23.5);
if (firstSafePlanet === undefined) {
return firstSafePlanet;
} else {
return firstSafePlanet + "%";
}
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("findSafeOxygenLevel function works - case 1", () => {
Expand Down
7 changes: 6 additions & 1 deletion 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
*/

function isBushSafe(berryArray) {
//Write your code here
let safeToEat = berryArray.every((color) => color === "pink");
if (safeToEat) {
return "Bush is safe to eat from";
} else {
return "Toxic! Leave bush alone!";
}
Copy link

@migmow migmow Mar 31, 2023

Choose a reason for hiding this comment

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

Nice one!
Two things to do if you're interested in learning more!
1- using some() and every() method together as the instruction says
2- when you have if else statement you can remove else, and just return what is supposed to be in else. Please research about it. It cleans up your code!

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

*/

function getSettlers() {}
function getSettlers(voyagers) {
return voyagers.filter(
(family) => family.startsWith("A") && family.includes("family")
Copy link

Choose a reason for hiding this comment

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

Again startsWith is really handy here, but there's another simple way to get the first index of word and see if it is equal to "A"

);
}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}
function getEligibleStudents(students) {
return students
.filter((student) => student[1] >= 8)
.map((student) => student[0]);
}

Copy link

Choose a reason for hiding this comment

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

Well done for using filter and map

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
20 changes: 14 additions & 6 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
if (stringText.includes(magicWord)) {
return stringText.indexOf(magicWord);
} else {
return "Not found";
}
Expand Down Expand Up @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}
function getTransportModes(array) {
return array.slice(1);
}

/*
Implement the function isAccessibleByTransportMode that
Expand All @@ -81,7 +83,9 @@ function getTransportModes() {}

Hint: Use the corresponding array method to decide if an element is included in an array.
*/
function isAccessibleByTransportMode() {}
function isAccessibleByTransportMode(modes, transportMode) {
return modes.includes(transportMode);
}

/*
Implement the function getLocationName that
Expand All @@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {}
- Returns the name of the location
e.g: "Tower Bridge"
*/
function getLocationName() {}
function getLocationName(locationAndTransports) {
return locationAndTransports[0];
}

/*
We arrived at the final method. it won't take long if you use the previously implemented functions wisely.
Expand Down Expand Up @@ -122,7 +128,9 @@ function getLocationName() {}
Advanced challange: try to use arrow function when invoking an array method.
*/
function journeyPlanner(locations, transportMode) {
// Implement the function body
return locations
.filter((location) => location.includes(transportMode))
.map((location) => location[0]);
}
Copy link

Choose a reason for hiding this comment

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

This is fine Andrius, you have already defined a funcition above called getLocationName, can you think how you can use this function in line 133?


/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 3 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
HINT: string and array methods that could be helpful (indexOf, filter)
*/

function getLanes() {}
function getLanes(streetNames) {
return streetNames.filter((street) => street.indexOf("Lane") > 0);
}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
Loading