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

// Example 1
let a;
console.log(a);
console.log(a); // Variable a hasn't been assigned a value.


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

let hello = sayHello();
let hello = sayHello(); // Function sayHello is not returning anything, so hello variable is assigned to nothing, hence we get undefined.
console.log(hello);


Expand All @@ -28,9 +28,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser();// The function is missing argument, and is not defined what it will be returning.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]);// The index value is out of range, since the length of arr is 3 and the last element index is 2.
10 changes: 8 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,15 @@
*/

function evenNumbers(n) {
// TODO
let res = [];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nicely done Anthony!

let num = 0;
while(res.length < n) {
res.push(num);
num += 2;
}
return res.join(",");
}

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
6 changes: 5 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while(i < birthdays.length) {
if(birthdays[i].includes("July")) return birthdays[i];
i++;
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
8 changes: 7 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,13 @@
*/

function evenNumbersSum(n) {
// TODO
let sum = [];
let num = 0;
do {
sum.push(num)
num += 2;
} while (sum.length < n)
return sum.reduce((tot, num) => tot + num);
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
8 changes: 3 additions & 5 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) {
console.log(String.fromCharCode(97 + i));
i++;
}

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

// The output shouldn't change.
1 change: 1 addition & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const AGES = [
];

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

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

for(let tube of tubeStations) console.log(tube);

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for(let letter of str) console.log(letter.toUpperCase());
2 changes: 1 addition & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

function getTemperatureReport(cities) {
// TODO
return cities.map(town => `The temperature in ${town} is ${temperatureService(town)} 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.

Nice use of .map() 👍

}


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

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let rand = generateRandomNumber();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The function getRandomNumberGreaterThan50() invokes generateRandomNumber() only once.
The do/while loop then executes until the result of the generateRandomNumber() returns a value greater than zero.
Will the do/while loop ever complete?

do {
if(rand > 50) {
return rand;
}
} while(rand <= 50)
return rand;
}
console.log(getRandomNumberGreaterThan50());

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

Expand Down
22 changes: 13 additions & 9 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(title => {

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 .filter().
Filter is a predicate function , so you can also just return the result of a boolean evaluation and it should still work.
Well done

if(title.length <= 65) return title;
})
}

/*
Expand All @@ -14,24 +16,26 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let num = Math.min(...allArticleTitles.map(title => title.split(" ").length));

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 is pretty cool, well done

return allArticleTitles.filter(title => (title.split(" ").length === num) ? title : "").join("");
}

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter(titleWithNum => (/\d/.test(titleWithNum)) ? titleWithNum : "");
}

/*
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.
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.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let arr = allArticleTitles.map(title => title.split("").length);
return Math.round(arr.reduce((tot, num) => tot + num)/arr.length);
}


Expand Down
39 changes: 23 additions & 16 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,45 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
return closingPricesForAllStocks.map(priceArr => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Concise, I like it!

return Number((priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2));
})
}

/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
- Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- Returns an array containing the price change over the last 5 days for each stock.
For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days.
In this example it would be:
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
- Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- Returns an array containing the price change over the last 5 days for each stock.
For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days.
In this example it would be:
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
return closingPricesForAllStocks.map(priceArr => {
return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2));
})
}

/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
- Takes 2 parameters:
- the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- the STOCKS array
- Returns an array of strings describing what the highest price was for each stock.
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
- Takes 2 parameters:
- the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- the STOCKS array
- Returns an array of strings describing what the highest price was for each stock.
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
return closingPricesForAllStocks.map((priceArr, i) => {
return `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...priceArr).toFixed(2)}`;
})
}
console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Really well done on your solutions for this exercise Anthony 🔥



/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 3 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
Using a loop, complete the function below so it returns the factorial of the number being passed in.
*/

// Solved it using recursion instead of a loop

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👀 Much more elegant than the official solution 🤣

function factorial(input) {
// TODO
if(input === 1) return 1;
return input * factorial(input - 1);
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
20 changes: 17 additions & 3 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
Each title in the resulting array should be the highest rated book in its genre.
*/

function getHighestRatedInEachGenre(books) {
// TODO
}


/* ======= Book data - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -69,6 +66,23 @@ const BOOKS = [
},
]

function getHighestRatedInEachGenre(books) {
let bookTitles = [];
let cookingRate = [];
let nonFictionRate = [];
let childrenRate = [];
for(let book of books) {
if(book.genre === "cooking") cookingRate.push(book.rating);
if(book.genre === "non-fiction") nonFictionRate.push(book.rating);
if(book.genre === "children") childrenRate.push(book.rating);
}
for(let book of books) {
if(book.genre === "cooking" && book.rating === Math.max(...cookingRate)) bookTitles.push(book.title);
if(book.genre === "non-fiction" && book.rating === Math.max(...nonFictionRate)) bookTitles.push(book.title);
if(book.genre === "children" && book.rating === Math.max(...childrenRate)) bookTitles.push(book.title);
}
return bookTitles;
}

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the highest rated book in each genre", () => {
Expand Down
13 changes: 12 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
*/

function generateFibonacciSequence(n) {
// TODO
let sequence = [0, 1];
let num1 = 0;
let num2 = 1;
let num3 = 0;
for(let i = 0; i < n - 2; i++) {
num3 = num1 + num2;
sequence.push(num3);
num1 = num2;
num2 = num3;
}
return sequence;
}
console.log(generateFibonacciSequence(10))

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
Expand Down