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
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,11 +12,12 @@
// Example 1
let a;
console.log(a);

// The variable a has not been declared.

// Example 2
function sayHello() {
let message = "Hello";
// no 'return message' in this function.
}

let hello = sayHello();
Expand All @@ -26,6 +27,7 @@ console.log(hello);
// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
// user has no value/ not defined.
}

sayHelloToUser();
Expand All @@ -34,3 +36,4 @@ sayHelloToUser();
// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// arr[3] calls for the 4th number in the array, there are only 3 numbers in this array.
16 changes: 11 additions & 5 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/

function evenNumbers(n) {
// TODO
function evenNumbers(n) {
let i = 0;
let array =[];
while (i < n ) {
array.push(i * 2);
i++;
}
return array.join();

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!

}

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
console.log(evenNumbers(3)); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18
7 changes: 6 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,12 @@ 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 i = 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.

This is really well done.
Only comment is to please try and format/indent your code consistently. 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will do!

let array =[];
do{
array.push(i * 2 );
i++;
} while(i < n);
return array.reduce((a,b) => a + b, 0);
}

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


// 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.


3 changes: 3 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,9 @@ const AGES = [
];

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

/*
The output should look something like this:
Expand Down
12 changes: 10 additions & 2 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements).
A for-of loop is an easy way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements).
*/

// TODO Use a for-of loop to output each of the tube stations below.
Expand All @@ -11,6 +11,14 @@ let tubeStations = [
"Tottenham Court Road"
];

for (let value of tubeStations) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code works perfectly fine. Try and name your variables with meaningful names ; names like value and val could be more descriptive to convey its actual use

console.log(value);
}



// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
let str = "codeyourfuture";
for(let val of str) {
console.log(val.toUpperCase());
}
12 changes: 8 additions & 4 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.
We also already have a temperatureService function which will take a city as a parameter and return a temperature.

Implement the function below:
- take the array of cities as a parameter
- return an array of strings, which is a statement about the temperature of each city.
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO
function getTemperatureReport(usersCities ){
let result = [];
for(let i = 0; i < usersCities.length; i++ ){
let message= "The temperature in " + usersCities[i] + " is " + temperatureService(usersCities[i]) + " degrees";
// let message = "The temperature in " + usersCity[i] + " is " + temperatureService[i] + " degrees";
result.push(message);
} return result;
}


Expand Down
6 changes: 6 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,12 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let 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.

Really well done!

do{
i = generateRandomNumber();

}while(i <= 50)
return i ;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
43 changes: 35 additions & 8 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,60 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
var result = allArticleTitles.filter((n) => n.length <= 65)

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 and the conditional to check for results before returning it.
What would happen if there were no articles with a length less than 65 and someone else was using your function ? Do you think your function should always return an array, even if it is an empty array?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No.. It should probably return a message saying there are no articles that fit right?

if (result) {
return result;
}
}



/*
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)
*/
(you can assume words will always be separated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
var fewestWords = allArticleTitles[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.

Excellent algorithm with the for loop and initializing the fewestWords with the first element.
You could also use a for of loop, but your solution works!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will try it out though. Thanks!

for (let i = 0; i < allArticleTitles.length; i++) {
var element = allArticleTitles[i];
if (fewestWords.length > element.length) {
fewestWords = element;
}
}
return fewestWords;
};


/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
The editor of the FT has realized 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
// if(typeof allArticleTitles === "number") {
// return typeof allArticleTitles === "number";
// }
titleWithNum = [];
var hasNumber = /\d/;
for(let element of allArticleTitles) {
if (hasNumber.test(element)){
titleWithNum.push(element);}
} return 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.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let total = 0;
let sum;
for(let element of allArticleTitles){
total= total + element.length;
sum = Math.round(total/allArticleTitles.length);
}return sum;
}


Expand Down
40 changes: 37 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let total= [];
let tot= 0;
let sum = 0;
for(let element of closingPricesForAllStocks){
sum = element.reduce((a, b) => a + b);
tot = sum / 5;

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 .reduce() function.
The constant literal 5 is an example of a "magic number" that should be avoided. What would happen to this calculation if the number of prices changed from 5 to any other number?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It would be wrong.. Should I use .length? I will try and do it without magic numbers.

total.push(Math.round(tot * 100) / 100);
} return total
}

/*
Expand All @@ -48,7 +55,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 total = [];
let priceChange = 0;
for (let prices of closingPricesForAllStocks) {
let last = prices.slice(-1);
// console.log(last);
// console.log(prices[0]);
priceChange = last - prices[0];
// total.push(priceChange.toFixed(2));
total.push( Math.round(priceChange * 100) / 100);

// console.log(priceChange);
} return total;
}

/*
Expand All @@ -64,7 +82,23 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let newStocks = [];
let total = [];
let message = "";
let last;
for (let stock of closingPricesForAllStocks) {
stock.sort((a, b) => b - a);
// last = stock[0];
// total.push( Math.round(last * 100) / 100);
total.push(stock);
}
for (let i = 0; i < stocks.length; i++) {
let roundedUp =(total[i][0]).toFixed(2);
// let roundedUp = Math.round((total[i][0]) * 100) / 100;
message = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + roundedUp;
newStocks.push(message);
}
return newStocks;
}


Expand Down