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
13 changes: 5 additions & 8 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@

// Example 1
let a;
console.log(a);

console.log(a); // return undefined because a variable that has not been assigned a value

// Example 2
function sayHello() {
let message = "Hello";
}

let hello = sayHello();
console.log(hello);

console.log(hello); // return undefined because value (message variable) was not returned.

// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
console.log(`Hello ${user}`); // return undefined because when call the function it is not have a argument
}

sayHelloToUser();


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let arr = [1, 2, 3];
console.log(arr[3]); // return undefined because the value has not been assigned in index 3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good - you have answered all of these correctly.

14 changes: 12 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
*/

function evenNumbers(n) {
// TODO
let count = 0;
let result = [];
if (n) {
while (count < n) {
// count === 0 ? result.push(count) : result.push(count * 2);
result.push(count * 2);
count++;
}
}
console.log(result);
console.log("--------------");
}

evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18
23 changes: 20 additions & 3 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,28 @@ const BIRTHDAYS = [
"July 11th",
"July 17th",
"September 28th",
"November 15th"
"November 15th",
];

// let July1 = BIRTHDAYS.filter((x) => x.split(" ")[0] === "July");
// console.log(July1.sort()[0]);

// Good use of split and index here.You could also use "break"
// to exit the
// while loop once you have found the first July date.

function findFirstJulyBDay(birthdays) {
// TODO
let july = [];
let count = 0;

while (count < birthdays.length) {
if (birthdays[count].split(" ")[0] === "July") {
july.push(birthdays[count]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of split and index here. You could also use "break" to exit the while loop once you have found the first July date.

break;
}
count++;
}
return july.sort()[0];
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
21 changes: 20 additions & 1 deletion 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@

Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/
//💫
// You need to re-read the question. You need the loop to add up the first n even numbers,
// starting at 0. So if n=3, you need to add up 0+2+4. If n=10 you
// need to add up 0+2+4+6+8+10+12+14+16+18.
//💫

function evenNumbersSum(n) {
// TODO
let result = [0];
// for (let i = 1; i < n; i++) {
// result[i] = i * 2 + result[i - 1];
// }
// let total = result[result.length - 1];
// return total;

let total;
let i = 1;
do {
result[i] = i * 2 + result[i - 1];
total = result[result.length - 1];
i++;
} while (i < n);
return total;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
15 changes: 9 additions & 6 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
Change the while loop below into a for loop.
*/


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
i++;
}
// let i = 0;
// while (i < 26) {
// console.log(String.fromCharCode(97 + i));
// i++;
// }
// The output shouldn't change.

for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(65 + i), String.fromCharCode(97 + i));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work - adding upper case letters wasn't necessary, but it's good to see that you are interested in how fromCharCode works.

18 changes: 7 additions & 11 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ const WRITERS = [
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya"
]

const AGES = [
59,
40,
41,
63,
49
"Yukiko Motoya",
];

// TODO - Write for loop code here
const AGES = [59, 40, 41, 63, 49];

// TODO - Write for loop code here
for (let i = 0; i < WRITERS.length; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, good use of the loop index for both arrays.

/*
The output should look something like this:

Expand All @@ -36,4 +32,4 @@ Zadie Smith is 40 years old
Jane Austen is 41 years old
Bell Hooks is 63 years old
Yukiko Motoya is 49 years old
*/
*/
15 changes: 12 additions & 3 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ let tubeStations = [
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Tottenham Court Road",
];


for (let letter of tubeStations) {
console.log(letter);
}
console.log("_____________________");
// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
let capitalise = "";
for (let letter of str) {
capitalise += letter.toUpperCase();
console.log(letter.toUpperCase());
}
console.log("_____________________");
console.log(capitalise);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good you have clearly understood how to use a for loop on an array.

58 changes: 36 additions & 22 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,65 @@
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/
// let usersCities = [
// "London",
// "Paris",
// "Barcelona",
// "Dubai",
// "Mumbai",
// "São Paulo",
// "Lagos",
// ];

function getTemperatureReport(cities) {
// TODO
let city = temperatureService;
let statementTempCity = cities.map(
(element) => `The temperature in ${element} is ${city(element)} degrees`
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, this is very efficient.

return statementTempCity;
}

/////////////////////////////////////////////////////////////
// function getTemperatureReport(cities) {
// let city = temperatureService;
// return cities.forEach((element) =>
// console.log(`The temperature in ${element} is ${city(element)} degrees`)
// );
// }
// getTemperatureReport(usersCities);

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

function temperatureService(city) {
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
let temparatureMap = new Map();

temparatureMap.set("London", 10);
temparatureMap.set("Paris", 12);
temparatureMap.set("Barcelona", 17);
temparatureMap.set("Dubai", 27);
temparatureMap.set("Mumbai", 29);
temparatureMap.set("São Paulo", 23);
temparatureMap.set("Lagos", 33);

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]
let usersCities = ["London", "Paris", "São Paulo"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees"
"The temperature in São Paulo is 23 degrees",
]);
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
"Dubai"
]
let usersCities = ["Barcelona", "Dubai"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees"
"The temperature in Dubai is 27 degrees",
]);
});

Expand Down
16 changes: 14 additions & 2 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
}
// let randomNum = generateRandomNumber;

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
}
let i = 100;
// let randomNumber = generateRandomNumber;
do {
let random = generateRandomNumber();
if (random > 50) {
return random;
}

i--;
} while (i > 50);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will exit after 50 loops, so it might not always find a number over 50. But I also understand why you want it to exit so that you don't end up with an endless loop if your logic is wrong.

}
// console.log(getRandomNumberGreaterThan50());
// getRandomNumberGreaterThan50();
/* ======= TESTS - DO NOT MODIFY ===== */

test("Returned value should always be greater than 50", () => {
Expand All @@ -21,4 +33,4 @@ test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
});
});
Loading