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

// Variable a has not been initialised/assigned

// Example 2
function sayHello() {
Expand All @@ -21,6 +21,7 @@ function sayHello() {

let hello = sayHello();
console.log(hello);
// The function sayHello() doesn't return any value


// Example 3
Expand All @@ -29,8 +30,10 @@ function sayHelloToUser(user) {
}

sayHelloToUser();
// When the function is called (last line), no argument is given (for its user parameter)


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// Because of zero-indexing, arr[3] tries to find unsuccessfully the 4th element in the array arr (the array has only 3 elements)
70 changes: 70 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,80 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/

// // Problematic solution (the numbers do not go in one line and there is a comma (,) after the last number)
// function evenNumbers(n) {
// // TODO
// let i = 0;
// while (i < n * 2) {
// if (i % 2 === 0) {
// console.log(i + ",");
// i++;
// } else {
// i++;
// }
// }
// }



// function evenNumbers(n) {
// // TODO
// let i = 0;
// let string = ""
// while (i < n * 2) {
// if (i % 2 === 0) {
// string = string + i + ", ";
// i++;
// } else {
// i++;
// }

// }
// string = string.slice(0,-2)
// // string = string.substring(0, string.length - 2) // Alternative but unnecessarily complicated
// console.log(string);
// }
// OK. This solution is correct but there must be a smarter way to do this than having to use slice...

// // Solution with the use of an array
// function evenNumbers(n) {
// // TODO
// let i = 0;
// let array = []
// while (i < n * 2) {
// if (i % 2 === 0) {
// array.push(i);
// i++;
// } else {
// i++;
// }
// }
// console.log(array);
// }




// Completely unnecessary use of %; overcomplicated problem-solving
function evenNumbers(n) {
// TODO
let i = 0;
let string = ""
while (i < n) {
string = string + (i * 2) + ", ";
i++;
}
string = string.slice(0,-2)
// string = string.substring(0, string.length - 2) // Alternative but unnecessarily complicated
console.log(string);
}



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




6 changes: 6 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,12 @@ const BIRTHDAYS = [

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

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

function evenNumbersSum(n) {
// TODO
let sum = 0
let i = 0
do {
sum += i;
i += 2;
}
while (i < n * 2);
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
9 changes: 6 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,12 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
// 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));
i++;
}
// The output shouldn't change.
5 changes: 5 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ const AGES = [
49
];


// 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
8 changes: 8 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,14 @@ let tubeStations = [
"Tottenham Court Road"
];

for (let 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 (string of str) {
console.log(string.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 @@ -13,9 +13,13 @@

function getTemperatureReport(cities) {
// TODO
let cityStatement = []
for (let city of cities) {
cityStatement.push("The temperature in " + city + " is " + temperatureService(city) + " degrees")
}
return cityStatement;
}


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

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

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
do {
x = generateRandomNumber();
}
while (x <= 50);
// console.log(x);
return x;
}

// getRandomNumberGreaterThan50();

// Quite enlightening exercise to realise that looping is not only about having a variable periodically increasing/decreasing in order to achieve something
// It's more about a condition and its 'truthiness' (or 'falsiness') in which case a body of code has to be executed
// This applies specially to the while

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

test("Returned value should always be greater than 50", () => {
Expand Down
58 changes: 58 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let articleTitlesUnder65 = []
for (let title of allArticleTitles) {
if (title.length <= 65) {
articleTitlesUnder65.push(title)
}
}
return articleTitlesUnder65
}

/*
Expand All @@ -15,6 +22,37 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
// let word = 0
// do {
// countingWords(allArticleTitles[word]
// )
// }

let articleTitle = 0;
let shortestTitle = 0
let shortestTitleWords = countingWords(allArticleTitles[articleTitle]);
for (let articleTitle = 1; articleTitle < allArticleTitles.length; articleTitle++){
if (countingWords(allArticleTitles[articleTitle]) < shortestTitleWords) {
shortestTitleWords = countingWords(allArticleTitles[articleTitle]);
shortestTitle = articleTitle;
}
}

return allArticleTitles[shortestTitle];
}

function countingWords(string) {
let numberOfSpaces = 0
let b = string.length
let character = 0
while (character < b) {
if (string[character] === " ") {
numberOfSpaces++
}
character++;
}
let numberOfWords = numberOfSpaces + 1;
return numberOfWords;
}

/*
Expand All @@ -24,14 +62,34 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let arrayNumbers = [];
for (let article of allArticleTitles) {
if (containsNumbers(article) === true) {
arrayNumbers.push(article);
}
}
return arrayNumbers;
}

function containsNumbers(str) {
return /\d/.test(str);
}



/*
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 sample = allArticleTitles.length
let sum = 0
for (let article = 0; article < sample; article++) {
sum = sum + allArticleTitles[article].length;
}
return Math.round(sum / sample);

}


Expand Down
42 changes: 42 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 averageArray = []
for (let average of closingPricesForAllStocks) {
averageArray.push(getAverage(average));
}
return averageArray
}

function getAverage(pricesOfEachStock) {
let sum = 0;
for (let price of pricesOfEachStock) {
sum = sum + price
}
return Number((sum / 5).toFixed(2));
}

/*
Expand All @@ -49,8 +62,16 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let differenceArray = []
for (let difference of closingPricesForAllStocks) {
differenceArray.push(Number((difference[difference.length-1] - difference[0]).toFixed(2))); // Definitely this is supercomplicated
}
return differenceArray;

}



/*
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 @@ -65,6 +86,27 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let theHighestPrice = highestPrice(closingPricesForAllStocks);
console.log(theHighestPrice)
let report = [];
let i = 0;
for (let stock of stocks) {
report.push("The highest price of " + stock.toUpperCase() + " in the last 5 days was " + theHighestPrice[i])
i++;
}
return report;

}

function highestPrice(closingPrices) {
highestPrices = [];
for (let price of closingPrices) {
highestPrices.push(/*Number*/((Math.max.apply(null, price)).toFixed(2))) // Found this solution here: https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max Please write about finding max number in array. I found the easiest way for me to find max number in array using spread syntax.
const arr = [1, 2, 3];
const max = Math.max(...arr);

// It works but I don't understand it - If I kept the Number method it would show the last higestPrice with one decimal after the point; not two...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When you have Number and you're using toFixed(2) method on Number. You will have 2 decimal number after dot only if they are not including zero at the end.
Examples of numbers:
10.563 => 10.56
10.100 => 10.1

When you have String - you will have two decimalsafter dot in any case. But the number will be a String.
"10.563" => "10.56"
"10.100" => "10.10"

So that's why it is better to leave the highest price as a String. Because in test we have a price "1101.30", not "1101.3". And we should only to show the price in sentence, we don't have any test which is checking if the price is a number.

// highestPrices.push(Math.max(price)) This didn't work
}
return highestPrices;
console.log(highestPrices)
}


Expand Down