Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
8 changes: 4 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1
// Example 1 - variable has not been assigned
let a;
console.log(a);


// Example 2
// Example 2 - value in the function was not returned
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +23,14 @@ let hello = sayHello();
console.log(hello);


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

sayHelloToUser();


// Example 4
// Example 4 - array does not have index number 3
let arr = [1,2,3];
console.log(arr[3]);
12 changes: 11 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
let j = 0;
let res = "";
while (i < n) {
if (j % 2 === 0) {
res += j + ",";
i++;
}
j++;
}
console.log(res.slice(0, -1));
}

evenNumbers(3); // should output 0,2,4
Expand Down
8 changes: 7 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ const BIRTHDAYS = [
"November 15th"
];


function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
const month = "July";
while (!(birthdays[i].includes(month))) {
i++;
}
return birthdays[i];
}

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

function evenNumbersSum(n) {
// TODO
let i = 0;
let j = 0;
let sum = 0;
do {
if (j % 2 === 0) {
sum += j;
i++;
}
j++;
} while (i < n);
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.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const AGES = [
49
];

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

/*
The output should look something like this:
Expand Down
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 (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 char of str) {
console.log(char.toUpperCase());
}
6 changes: 5 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/

function getTemperatureReport(cities) {
// TODO
let res = [];
for (const city of cities) {
res.push(`The temperature in ${city} is ${temperatureService(city)} degrees`);
}
return res;
}


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

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let i = 0;
do {
i = generateRandomNumber();
} while (i < 51);
return i;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
24 changes: 20 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let shortArticles = allArticleTitles.filter(title => title.length < 66);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice usage of .filter() 👍

return shortArticles;
}

/*
The editor of the FT likes short headlines with only a few words!
Implement the function below, which returns the title with the fewest words.
(you can assume words will always be seperated by a space)
*/

function titleWithFewestWords(allArticleTitles) {
// TODO
let lengthOfTitles = [];
for (const articleTitle of allArticleTitles) {
lengthOfTitles.push(articleTitle.split(" ").length - 1);
}
const min = Math.min(...lengthOfTitles);
const index = lengthOfTitles.indexOf(min);
return allArticleTitles[index];
}

/*
Expand All @@ -23,15 +31,23 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let digits = /\d+/;
let articlesWithNumbers = allArticleTitles.filter(articles => articles.match(digits));
return articlesWithNumbers;
}


/*
The Financial Times wants to understand what the average number of characters in an article title is.
Implement the function below to return this number - rounded to the nearest integer.
*/
let sumOfTitlesLength = 0;
function averageNumberOfCharacters(allArticleTitles) {
// TODO
for (const articleTitle of allArticleTitles) {
sumOfTitlesLength += articleTitle.length;
}
let averageChar = sumOfTitlesLength / allArticleTitles.length;
return Math.round(averageChar);
}


Expand Down
37 changes: 34 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let allAveragePrices = [];

for (const closingPricesForStock of closingPricesForAllStocks) {
let sum = 0;
let average = 0;

for (const price of closingPricesForStock) {
sum += price;
}
average = sum / closingPricesForStock.length;
allAveragePrices.push(parseFloat(average.toFixed(2)));
}
return allAveragePrices;
}

/*
Expand All @@ -48,7 +60,18 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let changeInPrice = [];
let firstPrice = 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.

What is the benefit of defining these variables outside of the for loop?

let lastPrice = 0;
let difference = 0;

for (const closingPricesForStock of closingPricesForAllStocks) {
firstPrice = closingPricesForStock[0];
lastPrice = closingPricesForStock[closingPricesForAllStocks.length - 1];
difference = lastPrice - firstPrice;
changeInPrice.push(parseFloat(difference.toFixed(2)));
}
return changeInPrice;
}

/*
Expand All @@ -64,7 +87,15 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPriceForAllStock = [];
let highestPrice = 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.

What's the benefit of defining this variable outside of the for loop?


for (let i = 0; i < closingPricesForAllStocks.length; i++) {
highestPrice = Math.max(...closingPricesForAllStocks[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.

Nice use of the spread operator 👍

highestPrice = highestPrice.toFixed(2);
highestPriceForAllStock[i] = `The highest price of ${stocks[i].toUpperCase()} in the last ${closingPricesForAllStocks.length} days was ${highestPrice}`;
}
return highestPriceForAllStock;
}


Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

function factorial(input) {
// TODO
let sum = 1;
for (let i = input; i >= 1; i--) {
sum *= i;
}
return sum;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
12 changes: 11 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
let sortedBooks = books.sort((b1, b2) => b1.rating < b2.rating ? 1 : b1.rating > b2.rating ? -1 : 0);
let highestRatedBook = [];
let arrOfGenres = [];

for (const book of sortedBooks) {
if (!arrOfGenres.includes(book.genre)) {
arrOfGenres.push(book.genre);
highestRatedBook.push(book.title);
}
}
return highestRatedBook;
}


Expand Down
9 changes: 8 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
*/

function generateFibonacciSequence(n) {
// TODO
sequence = [0, 1];
let newNum = 0;

for (let i = 2; i < n; i++) {
newNum = sequence[i - 2] + sequence[i - 1];
sequence.push(newNum);
}
return sequence;
}

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