Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
8 changes: 8 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
let a;
console.log(a);

// the variable a hasn't been initialised i.e. it's not been assigned an initial value


// Example 2
function sayHello() {
Expand All @@ -22,6 +24,7 @@ function sayHello() {
let hello = sayHello();
console.log(hello);

// the function won't return anything, so the variable hello will return undefined.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nitpick: the variable hello will be undefined, try to avoid using the word return for variables and just use it for functions.


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

sayHelloToUser();

// the function needs an argument called user to be passed through it.
// in line 34, there are no arguments being passed through the function so it will return Hello undefined.

// Example 4
let arr = [1,2,3];
console.log(arr[3]);

// so the array begins at index 0 and ends at index 2.
// there is no value assigned at index 3 so it'll return undefined.
4 changes: 2 additions & 2 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Declare some variables assigned to arrays of values
*/

let numbers = []; // add numbers from 1 to 10 into this array
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array
let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length - 1]; // complete this statement
}

/*
Expand Down
2 changes: 2 additions & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration
numbers.push(4);
numbers[0] = 1;

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
7 changes: 7 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const AGES = [

// TODO - Write for loop code here

for (i = 0; i < WRITERS.length; 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.

Issue: The iterator variable i in a for loop should be declared with let.

let writersName = WRITERS[i];
let writersAge = AGES[i];

console.log(`${writersName} is ${writersAge} years old!`);
}

/*
The output should look something like this:

Expand Down
12 changes: 10 additions & 2 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ const BIRTHDAYS = [
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"July 17th",-
"September 28th",
"November 15th"
];

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"
13 changes: 11 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
*/

function getTemperatureReport(cities) {
// TODO
}
let temperatureReportResults = [ ];

for (i = 0; i < cities.length; 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.

Suggestion: Use a for (city of cities) loop here. This reduces the amount of code you need to write and it is easier for a reader to understand your intention.

let city = cities[i];
let temperature = temperatureService(city);
let reportPhrasing = `The temperature in ${city} is ${temperature} degrees`;

temperatureReportResults.push(reportPhrasing);
}

return temperatureReportResults;
}

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

Expand Down
55 changes: 49 additions & 6 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/*
Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array.
Imagine you are working on the Financial Times website! They have a list of article titles stored in an array.

The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let titlesShortEnough = [ ];

for (const title of allArticleTitles) {
if(title.length <= 65) {
titlesShortEnough.push(title);
}
}

return titlesShortEnough;
}

/*
Expand All @@ -14,24 +22,59 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}

let smallestTitle = allArticleTitles[0];

for (i = 1; i < allArticleTitles.length; 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.

Issue: The iterator variable i in a for loop should be declared with let. For example

for (let i = 1; i < allArtivlesTitles.length; i++)

Suggestion: Use a for...of loop instead of a for loop to improve readability


if (smallestTitle.split(" ").length > allArticleTitles[i].split(" ").length) {
smallestTitle = allArticleTitles[i];
}
}
return smallestTitle;
}
/*
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
let headlinesWithNumbers = [ ];
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (i = 0; i < allArticleTitles.length; 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.

Issue: The iterator variable i in a for loop should be declared with let.
Suggestion: Use a for...of loop instead of a for loop to improve readability

if(numbers.some(element => allArticleTitles[i].includes(element))) {
headlinesWithNumbers.push(allArticleTitles[i]);
}
}
return headlinesWithNumbers;
}

/*
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
// To store the total characters per title
let totalCharactersPerTitle = [ ];

// To count the characters per title and add them to totalCharactersPerTitle
for (i = 0; i < allArticleTitles.length; 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.

Issue: The iterator variable i in a for loop should be declared with let.
Suggestion: Use a for...of loop instead of a for loop to improve readability

let countCharacters = allArticleTitles[i].length;
totalCharactersPerTitle.push(countCharacters);
}

// To store the sum of total characters
let totalCharacters = 0;
totalCharactersPerTitle.forEach( item => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: This implementation is fine, but this could also be done with a reducer. e.g.

let totalCharaters = totalCharactersPerTitle.reduce((iterator, element) => iterator + element);

totalCharacters += item;
})

// To work out the average and round to the nearest integer
let averageNumberOfCharacters = Math.round(totalCharacters / (allArticleTitles.length));

return averageNumberOfCharacters;
}


Expand Down
63 changes: 58 additions & 5 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :)

Imagine we a working for a finance company. Below we have:
Imagine we are working for a finance company. Below we have:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Praise: good spot :)

- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
Expand Down Expand Up @@ -34,7 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
// for storing averages of each company's stock over 5 days
let averagePriceOverFiveDays = [ ];

// This loop cycles through each stock, the i is the 5 day stock for AAPL, MSFT etc.
for (i = 0; i < closingPricesForAllStocks.length; 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.

Issue: The iterator variable i in a for loop should be declared with let
Suggestion: Use a for...of loop instead of a for loop to improve readability

let totalStock = 0;
closingPricesForAllStocks[i].forEach((element) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: Instead of doing * 100 on the calculated average at the end of the function do a * 100 on each element when adding it to totalStock. This lets you do more of the math with integers which reduces the floating point inaccuracies caused by math with floating point numbers.

totalStock += element;
});
let calculateAverage = totalStock / closingPricesForAllStocks[i].length;
let roundAverage = Math.round(calculateAverage * 100) / 100;
averagePriceOverFiveDays.push(roundAverage);
}

return averagePriceOverFiveDays;
}

/*
Expand All @@ -47,10 +61,28 @@ function getAveragePrices(closingPricesForAllStocks) {
(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


function getPriceChanges(array) {

let changeInPrice = [ ];

for (i = 0; i < array.length; 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.

Issue: The iterator variable i in a for loop should be declared with let.
Suggestion: Use a for...of loop instead of a for loop to improve readability

let closingPricesArray = array;
let lengthOfInnerArray = closingPricesArray[i].length;
let fifthDayPrice = closingPricesArray[i][lengthOfInnerArray - 1];
let firstDayPrice = closingPricesArray[i][0];

let fifthDayMinusFirstDayRounded = Math.round((fifthDayPrice - firstDayPrice) * 100) / 100;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion do * 100 on fifthDayPrice and firstDayPrice separately so the - operation will occur on integers instead of floating point numbers, reducing floating point math inaccuracy.


changeInPrice.push(fifthDayMinusFirstDayRounded);
}

return changeInPrice;
}



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

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let currentMax = closingPricesForAllStocks[i][0];

for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
let currentElement = closingPricesForAllStocks[i][j];

if (currentElement >= currentMax) {
currentMax = currentElement;
}
}

let capitaliseStockNames = stockNames[i].toUpperCase();

maxPrices.push(currentMax.toFixed(2));
maxStockDescriptions.push(`The highest price of ${capitaliseStockNames} in the last 5 days was ${maxPrices[i]}`)
}

return maxStockDescriptions;
}


Expand Down