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
9 changes: 5 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@
*/

// Example 1
// We didn't give a value to a variable a
let a;
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; // we have to return something
}

let hello = sayHello();
console.log(hello);
console.log(hello); // we need quotation marks


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

sayHelloToUser();
sayHelloToUser(); // there is not argument


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // there are only 3 elements in the array. The last index should be 2
13 changes: 11 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,18 @@
*/

function evenNumbers(n) {
// TODO
if (n === 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice implementation! :)
Just keep an eye on indentation, to make the code more readable.

console.log("nothing")
} else {
let i = 0;
let newString = "";
while (i < n) {
newString = newString + 2*i + ",";
i++;
}
console.log(newString.substring(0, newString.length-1));
}
}

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
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 (birthdays[i].substring(0, 4) !== "July") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is works and is perfectly fine.
Another way to achieve the same thing here is using the String startsWith method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

i++;
}
return birthdays[i];
}

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

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

while (i < n);
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
6 changes: 6 additions & 0 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@


// 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.
7 changes: 7 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,13 @@ const AGES = [
];

// 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
9 changes: 9 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,15 @@ 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 (let letter of str) {
console.log(letter.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 answer = [];
for (let i = 0; i < cities.length; i ++) {
answer.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees")
}
return answer;
}


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 randomNum = 0;
do {
randomNum = generateRandomNumber();
} while (randomNum <=50);
return randomNum;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
36 changes: 32 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let newArticle = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The implementation here is good. A couple of minor points - indentation in the body of the if-statement would improve readability.
Also, maybe the variable name newArticle could be better, as it is an array that will hold multiple articles.

for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
newArticle.push(allArticleTitles[i])
}
}
return newArticle;
}

/*
Expand All @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let minWord = allArticleTitles[0].split(" ").length;
let j = 0;
for (let i = 0; i < allArticleTitles.length; i++ ) {
if (allArticleTitles[i].split(" ").length < minWord) {
minWord = allArticleTitles[i].split(" ").length;
j = i;
}

}
return allArticleTitles[j];

}

/*
Expand All @@ -23,15 +39,27 @@ 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 arrNum = [];
for (let i = 0; i < allArticleTitles.length; i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is an interesting implementation :)
You could make this a little bit more efficient using the break keyword. Basically, in the inner loop - if you've already added the current word to the array, you can break out of the loop, and just carry on to the next word.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break

for (let j = 0; j <= 9; j++) {
if (allArticleTitles[i].includes(j) && !arrNum.includes(allArticleTitles[i])) {
arrNum.push(allArticleTitles[i]);
}
}
}
return arrNum;
}

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


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice implementation :)

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let sumAverage = 0;
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
sumAverage = sumAverage + closingPricesForAllStocks[i][j];
}
averageArr.push(Math.round(sumAverage/closingPricesForAllStocks[i].length * 100) / 100);
}
return averageArr;
}

/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -48,7 +56,11 @@ 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 priceChange = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
priceChange.push(Math.round((closingPricesForAllStocks[i][closingPricesForAllStocks[i].length-1] - closingPricesForAllStocks[i][0]) * 100) / 100);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is good, but it would be a good idea to split this into multiple lines to improve the readability of the code.

}
return priceChange;
}

/*
Expand All @@ -64,7 +76,18 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let maxPrice = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good job!

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = closingPricesForAllStocks[i][0];
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
if (closingPricesForAllStocks[i][j] > highestPrice) {
highestPrice = closingPricesForAllStocks[i][j];
}
}
let str = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestPrice.toFixed(2);
maxPrice.push(str);
}
return maxPrice;
}


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 production = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very good :)

for (let i = 1; i <=input; i++ ) {
production = production * i;
}
return production;
}

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

function getHighestRatedInEachGenre(books) {
// TODO

}


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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also very good :)

for (let i = 0; i < n-2; i++ ) {
fibArr.push(fibArr[fibArr.length-1] + fibArr[fibArr.length-2]);

}
return fibArr;
}

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