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

London10-Afsha-Hossain-JS1-Week4 #236

Open
wants to merge 3 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
40 changes: 40 additions & 0 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -17,9 +20,46 @@ let names = [
"Ahmed",
];

function isLongName(singleName) {
return singleName.length > 7;
}

let longName = names.find(isLongName);

Choose a reason for hiding this comment

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

Good use of find function. FYI it is also possible to write line 23-27 into one statement. The benefit is that you can save the effort on thinking a function name isLongName. It is a personal coding style, not a rule to follow though.

let longName = names.find((name) => singleName.length > 7);


function isStartingWithA(singleName) {
return singleName[0] === "A";
}

let nameStartingWithA = names.find(isStartingWithA);

function findLongNameThatStartsWithA(names) {

Choose a reason for hiding this comment

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

The names parameter is being used inside the function, do you think it can be removed?

To write the code simpler, on line 40 we can do the comparison directly as there is just one line of code.

let longNameThatStartsWithA = nameStartingWithA && longName;

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
// }
6 changes: 5 additions & 1 deletion 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
7 changes: 6 additions & 1 deletion 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

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

Good use of every. What is the value of list? is it the element of group like "Austine", "Dany"... ?
on line 11 if we want to check if the name from the group is included in students array, it should be like students.includes right?

}

if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand All @@ -16,3 +20,4 @@ if (groupIsOnlyStudents) {
/* EXPECTED RESULT */

// The group does not contain only students

2 changes: 1 addition & 1 deletion 1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion 1-exercises/F-array-forEach/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
13 changes: 13 additions & 0 deletions 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/*
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(); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 3 additions & 1 deletion 1-exercises/G-array-methods/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

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

Good use of concat, FYI to combine two arrays it can also be done as below

let everyone = [...mentors, ...students];

// let everyone = [...mentors, ...students]; // alternate solution

/*
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); // complete this statement
let lastFive = everyone.slice(2, 7); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
23 changes: 22 additions & 1 deletion 1-exercises/H-array-methods-2/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
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); // complete this statement
}

/*
Expand Down
3 changes: 2 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,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");

Choose a reason for hiding this comment

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

Good use of replace, FYI there is a way to replace all the occurrence with one replace function, in this case we want to replace dogs into cats, it can be done by regular expression with the global flag g. If you are interested you can look it up.

story.replace(/dogs/g, 'cats')


/* 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
2 changes: 1 addition & 1 deletion 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 10 additions & 3 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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);
}

/*
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();
}

/*
Expand All @@ -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();

Choose a reason for hiding this comment

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

is singleString accessible here still?

}

/*
Expand All @@ -45,6 +51,7 @@ Write a function that:
*/

function formatPercentage() {

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
9 changes: 8 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Choose a reason for hiding this comment

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

It seems the if is missing

return element[0];
}
});
// let modifyArray = filterArray.map(element => element[0])
// return modifyArray
}

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

Expand Down
10 changes: 9 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {

Choose a reason for hiding this comment

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

As the hint said, indexOf would be useful to help check the partial match in the string.

laneNames.push(roadNames);
}
return laneNames;
}

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

Expand Down
15 changes: 14 additions & 1 deletion 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down