Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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
5 changes: 4 additions & 1 deletion 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ let names = [
"Karim",
"Ahmed",
];
//function longNameWithA(name) {
// return name.length > 7;
//}

let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For this exercise you should define your own function findLongNameThatStartsWithA and implement your logic there.

let longNameThatStartsWithA = names.find(name => name.length > 7);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You have the right idea here but just need to also add logic for starting with A


console.log(longNameThatStartsWithA);

Expand Down
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 @@ -11,7 +11,11 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
// If there is a null value in the array exit the program with the error code
// https://nodejs.org/api/process.html#process_process_exit_code
// process.exit(1);

for (let pair of pairsByIndex){
if (!pair){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think here it is better to use pair === null because that will check the type too

process.exit(1);
}
}
let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

Expand Down
14 changes: 13 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,19 @@
let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
/*function containsStudent (groupWithStudents){
for (let name of groupWithStudents){
if(group.includes(students)){
return true;
} else{
return false;
}

}

}*/

let groupIsOnlyStudents = students.every(name => group.includes(students)? true : false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you don't need the ternary operator ? because the .includes() function already returns a bool


if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand Down
13 changes: 12 additions & 1 deletion 1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@

let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"];

let pairsByIndex; // Complete this statement





let pairsByIndex = pairsByIndexRaw.filter(item => Array.isArray(item) && item.length === 2);







let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand Down
12 changes: 11 additions & 1 deletion 1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@

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

let numbersMultipliedByOneHundred; // complete this statement
/*let numbersMultipliedByOneHundred = numbers.map(function multiply(number) {
// return number * 100;
//});
let numbersMultipliedByOneHundred = numbers.map(function(number){
return number * 100;
})
let numbersMultipliedByOneHundred = numbers.map(number =>{

return number *100;
})*/
let numbersMultipliedByOneHundred = numbers.map(number => number *100);
console.log(numbersMultipliedByOneHundred);


/* EXPECTED RESULT

Expand Down
19 changes: 19 additions & 0 deletions 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
*/

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
arr.forEach(n => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

your logic here is good but I think you should take more care to indent your code more neatly

if(n % 3 === 0 && n % 5 === 0){
console.log("FizzBuzz");}
else if(n % 3 === 0){
console.log("Fizz");}
else if(n % 5 === 0){
console.log("Buzz");}
else{
console.log(n);
}



}
);





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

/*
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you can use negative number indexes with everyone.slice(-5)


/*
DO NOT EDIT BELOW THIS LINE
Expand Down
7 changes: 6 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,12 @@
Tip: use the string method .split() and the array method .join()
*/

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


/*
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
2 changes: 1 addition & 1 deletion 1-exercises/I-string-replace/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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","10000") .replace("great","brilliant") .replace("day","night");

/* 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(7,statement.length);

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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you could also do e.g. names[4].split(' ')[1] to solve this exercise, but your solution is good


names.forEach((name) => {
console.log(name);
Expand Down
5 changes: 1 addition & 4 deletions 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
HINT: You will need to use .substring() twice
*/

let statement = "I do not like programming";
Copy link
Copy Markdown

@jxz12 jxz12 Mar 25, 2023

Choose a reason for hiding this comment

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

you could also use .splice() to nolve this if you turn it into an array first with .split() them .join()


let result = "";

let statement = "I do not like programming"
console.log(result);

/* EXPECTED OUTPUT
Expand Down
35 changes: 29 additions & 6 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ 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(arr) {
let newArr =arr.slice(0,5);
return newArr;}


/*
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the same elements, except sorted.
*/
function sortArray() {
function sortArray(arr) {
return arr.slice().sort();
}

/*
Expand All @@ -24,7 +27,14 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(arr) {
newArr = arr.map(function (string) {
string = string.trim();
string = string.replace("/", "");
string = string.toLowerCase();
return string;
});
return newArr;
}

/*
Expand All @@ -33,7 +43,10 @@ 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) {
let arr =array.slice();
arr.splice(index,1);
return arr;
}

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

function formatPercentage() {
function formatPercentage(numbers) {
let newnumbers = numbers.map(function (number) {
if (number > 100) {
number = "100%";
} else {
number = `${Math.round(number * 100) / 100}%`;
}

return number;
});
return newnumbers;
}

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

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(oxygenLevels) {
let planets=oxygenLevels.filter(oxygenLevel =>oxygenLevel.includes("%"))
let numberLevels= planets.map(number => parseFloat(number))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would like better indentation here

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you could chain together the array methods without having to declare variables with let

let safePlanets=numberLevels.find(level => level> 19.5 && level<23.5)
if(safePlanets!==undefined){
return `${safePlanets}%`;
}

}



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

Expand Down
10 changes: 8 additions & 2 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
Let's first look at an example that will teach you how to use these methods.
*/

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

}
}

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

*/

function getSettlers() {}
function getSettlers(families) {
let family = families.filter(word=>word.startsWith("A") && word.endsWith("family"))
return family;
}

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

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

function getEligibleStudents() {}
function getEligibleStudents(students, attendance) {
let studentName = students.filter(attendance=>attendance[1]>=8)
return studentName.map(names=>names[0]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you could chain together filter and map with the extra let

}

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

Expand Down
Loading