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
9 changes: 5 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
For each example, can you explain why we are seeing undefined?
*/

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

// Answer: Its because there is no value for a.

// Example 2
function sayHello() {
Expand All @@ -21,16 +21,17 @@ function sayHello() {

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

// Answer: We didn't call the variable message so it won't output it.

// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();

// Answer:Because user is undefined

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// Answer: We only have 0,1,2 index and there is no value for index 3.
13 changes: 13 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

function evenNumbers(n) {
// TODO

let numbers = [];

let i = 0;

while (i < n) {

numbers.push(i * 2);

i++;
}

console.log(numbers.join(','));
}

evenNumbers(3); // should output 0,2,4
Expand Down
18 changes: 18 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,24 @@ const BIRTHDAYS = [

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


while (i < birthdays.length) {

let birthday = birthdays[i];


if (birthday.includes('July')) {

return birthday;
}

i++;
}

return null;

}

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

function evenNumbersSum(n) {
// TODO
let sum = 0;

for (let i = 0; i < n; i++) {

sum += i * 2;
}

return sum;

}

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


// 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 @@ -27,6 +27,13 @@ const AGES = [
];

// TODO - Write for loop code here
for (let i = 0; i < WRITERS.length; i++) {

let writer = WRITERS[i];
let age = AGES[i];

console.log(`${writer} is ${age} years old`);
}

/*
The output should look something like this:
Expand Down
11 changes: 11 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,17 @@ let tubeStations = [
"Tottenham Court Road"
];

for (const station of tubeStations) {

console.log(station);
}


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

for (const ch of str) {
console.log(ch.toUpperCase());
}


13 changes: 13 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@

function getTemperatureReport(cities) {
// TODO

let reports = [];

for (const city of cities) {

let temperature = temperatureService(city);

let report = `The temperature in ${city} is ${temperature} degrees`;

reports.push(report);
}

return reports;
}


Expand Down
7 changes: 7 additions & 0 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let num = generateRandomNumber();
do {
num = generateRandomNumber();
} while (num <= 50);

return num;

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
32 changes: 32 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averagePrices = [];
for (let stockPrices of closingPricesForAllStocks) {
let sum = 0;

for (let price of stockPrices) {
sum += price;
}

averagePrices.push(Math.round((sum / stockPrices.length) * 100) / 100);
}

return averagePrices;

}

/*
Expand All @@ -49,6 +62,14 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let priceChanges = [];

for (let stockPrices of closingPricesForAllStocks) {
priceChanges.push(Math.round((stockPrices[stockPrices.length - 1] - stockPrices[0]) * 100) / 100);
}

return priceChanges;

}

/*
Expand All @@ -65,6 +86,17 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPrices = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let stockPrices = closingPricesForAllStocks[i];
let maxPrice = Math.max(...stockPrices);

highestPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${maxPrice}`);
}

return highestPrices;


}


Expand Down