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
7 changes: 6 additions & 1 deletion 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// Example 1
let a;
console.log(a);

// Because there is no value is assigned to the variable a,

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


let hello = sayHello();
console.log(hello);
// Since the function sayHello is missing the return statement invoking will it result in undefined


// Example 3
Expand All @@ -30,7 +32,10 @@ function sayHelloToUser(user) {

sayHelloToUser();

// Since the function sayHelloToUser was defined with a parameter, however, invoking it will require an argument, and invoking it without an argument will result in undefined


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// This operation will result in undefined because there is no index[3] in the array
16 changes: 16 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@

function evenNumbers(n) {
// TODO
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let j = nums.slice(0, n);

let i = 0;

while( i < j.length){

console.log(j[i] * 2);
i ++;
}

return j;

}



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
10 changes: 10 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;


while(i < BIRTHDAYS.length){

if(BIRTHDAYS[i] === "July 11th") return "July 11th";

i++;
}

}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
13 changes: 13 additions & 0 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
*/

function evenNumbersSum(n) {

// TODO

let sums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sliced = sums.slice(0, n);
let sum = 0;

for(let slice of sliced){

sum += slice * 2;
}

return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
5 changes: 2 additions & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {

for(let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;
}
// The output shouldn't change.
7 changes: 7 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const AGES = [

// TODO - Write for loop code here

for(let i = 0; i < WRITERS.length; i++){

console.log(WRITERS[i].concat(' is ' + AGES[i] + " years old "));

}

/*
The output should look something like this:

Expand All @@ -36,4 +42,5 @@ Zadie Smith is 40 years old
Jane Austen is 41 years old
Bell Hooks is 63 years old
Yukiko Motoya is 49 years old

*/
7 changes: 7 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ let tubeStations = [
"Tottenham Court Road"
];

for(let tubeStation of tubeStations){

console.log(tubeStation.toUpperCase());
}

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


133 changes: 71 additions & 62 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
/*
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.

Implement the function below:
- take the array of cities as a parameter
- return an array of strings, which is a statement about the temperature of each city.
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO
}


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

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
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"
]);
});

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

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

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
/*
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.

Implement the function below:
- take the array of cities as a parameter
- return an array of strings, which is a statement about the temperature of each city.
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {

let temp = temperatureService(city);
cities.forEach((city) =>{

city = `The temperature in ${city} is ${temp} degrees`;
})

return city;

// TODO
}


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

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
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"
]);
});

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

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

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
});
59 changes: 35 additions & 24 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
/*
In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50.
Implement this using a do-while loop.
*/

// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
}

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

test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
});
/*
In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50.
Implement this using a do-while loop.
*/

// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop

let random = 0;

do{

random = generateRandomNumber();

}while(random < 50);

return random;

}

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

test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
});
Loading