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
6 changes: 6 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
let a;
console.log(a);

// The variable "a" is declared but no value is assigned to it.

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


let hello = sayHello();
console.log(hello);

// The function doesn't return a value.

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

sayHelloToUser();

// No value is passed for the function parameter "user".

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

// There is no value for array fourth element.
11 changes: 10 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

function evenNumbers(n) {
// TODO
}
let i = 0;
let even = [];
while (i < 2 * n) {

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!

even.push(i);
i += 2;
}
return even.toString();
}
console.log(evenNumbers());

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 logs out the array fine, looks good.
It's a minor point but the instruction is asking to log it out as a comma-separated string. Have a look at the array.join method.



evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
Expand Down
27 changes: 17 additions & 10 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
*/

const BIRTHDAYS = [
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th"
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th",
];

function findFirstJulyBDay(birthdays) {
// TODO
// TODO
let i = 0;
while (i < birthdays.length) {
if (birthdays[i][1] === "u") {

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 works, but what if later on more birthdays are added to the array including ones in June? Is it a bit more robust and readable to look for the whole word 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
// TODO
let i = 0;
let sum = n * (n - 1);
do {
return sum;

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 returning instantly from the evenNumbersSum function, so it only ever executes once. Line 12 (let sum) is working out the total on its own, so there's no need for the loop. Is there a way to work out the total by adding to the total on each iteration of the loop?

i++;
} while (i < 5);
}

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


// Change the below code to use a for loop instead of a while loop.
let i = 0;
/*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.
5 changes: 4 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const AGES = [
];

// TODO - Write for loop code here

for (i = 0; i < WRITERS.length; i++) {
let text = WRITERS[i] + " is " + AGES[i] + " years old";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great! Also have a look at string templates, see if you prefer that syntax.

console.log(text);
}
/*
The output should look something like this:

Expand Down
7 changes: 7 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,13 @@ 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 x of str){
console.log(x.toLocaleUpperCase());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I didn't know about toLocaleUpperCase! Good thinking 👏

}
3 changes: 1 addition & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

function getTemperatureReport(cities) {
// TODO
return cities.map(el => "The temperature in " + el + " is " + temperatureService(el) + " degrees");
}


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

function temperatureService(city) {
Expand Down
8 changes: 7 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ function generateRandomNumber() {
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
// TODO - implement using a do-while loop
let n;
do {
n = generateRandomNumber();
} while (n <= 50);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

return n;
}
console.log(getRandomNumberGreaterThan50());

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

Expand Down
32 changes: 30 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
// TODO
let articleArr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
articleArr.push(allArticleTitles[i]);
}
}
return articleArr;
}

/*
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)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let wordsCount= [];
for (let i = 0; i < allArticleTitles.length; i++){
wordArticle = allArticleTitles[i].split("");

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 words need to be split by a space ie " "

wordsCount.push(wordArticle.length);
}
let index = wordsCount.indexOf(Math.min(...wordsCount))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wow nice use of the spread operator ... to convert an array into a list of arguments 👏

return allArticleTitles[index];
}

/*
Expand All @@ -24,6 +37,14 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let numArticle = [];
let pattern =/[0-9]/g;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you know what the g suffix does? How will it affect the result of the search? (this all works by the way, I'm just asking ;)

for (let i=0; i<allArticleTitles.length; i++){
if (allArticleTitles[i].search(pattern)!= -1 ){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Better to prefer !== over != because !== gives more predictable results (!= will try to convert the types). Have a look for 'equality operator and type coercion' if interested but don't worry right now if not.

numArticle.push(allArticleTitles[i]);
}
}
return numArticle;
}

/*
Expand All @@ -32,6 +53,13 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let total = 0;
for (i=0; i < allArticleTitles.length; i++){
total += allArticleTitles[i].length;
average = total / allArticleTitles.length;
roundedAverage = Math.round(average);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perfect - will round to the closest integer, either upwards or downwards 👍

}
return roundedAverage;
}


Expand Down
26 changes: 25 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averageArr = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let total = 0;
for (let j=0; j < closingPricesForAllStocks[i].length; j++){
total += closingPricesForAllStocks[i][j];
}
let average = total / closingPricesForAllStocks[i].length;
let roundedAverage = Number(average.toFixed(2));

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 technique

averageArr.push(roundedAverage);

}
return averageArr;
}

/*
Expand All @@ -49,6 +61,12 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let changeArr=[];
for (let i = 0; i < closingPricesForAllStocks.length; i++){
let change = closingPricesForAllStocks[i][closingPricesForAllStocks.length - 1] - closingPricesForAllStocks[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 works 👍
It's a little hard to read, eg could break into separate lines:

const prices = closingPricesForAllStocks[i]
const firstPrice = prices[0]
const lastPrice = prices[prices.length - 1]
const change = lastPrice - firstPrice;

changeArr.push(Number(change.toFixed(2)));
}
return changeArr;
}

/*
Expand All @@ -64,7 +82,13 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
// TODO
let maxArray = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++){
let maxStocks = Math.max(...closingPricesForAllStocks[i]).toFixed(2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

toFixed returns a string, and Math.max expects a list of numbers

maxArray.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + maxStocks);
}
return maxArray;
}


Expand Down
11 changes: 11 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

function factorial(input) {
// TODO
let result = 1;
let i = input;
if (input = 0) {
return 1;
} else {
while (i > 0){
result *= i;
i--;
}
return result;
}
}

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

function getHighestRatedInEachGenre(books) {
// TODO
}
let childrenGenre = [];
let nonFictionGenre = [];
let cookingGenre = [];
let booksArr = [];
for (let i = 0; i < books.length; i++) {
if (books[i].genre === "children") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Works perfectly

childrenGenre.push(books[i]);
} else if (books[i].genre === "non-fiction") {
nonFictionGenre.push(books[i]);
} else {
cookingGenre.push(books[i]);
}
}
let childrenSorted = childrenGenre.sort((a, b) => a.rating - b.rating);

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! Also consider

const sorted = childrenGenre.sort().reverse()

let nonFictionSorted = nonFictionGenre.sort((a, b) => a.rating - b.rating);
let cookingSorted = cookingGenre.sort((a, b) => a.rating - b.rating);


booksArr.push(childrenSorted[childrenSorted.length - 1].title);
booksArr.push(cookingSorted[cookingSorted.length - 1].title);
booksArr.push(nonFictionSorted[nonFictionSorted.length - 1].title);

return booksArr;
}




/* ======= Book data - DO NOT MODIFY ===== */
Expand Down
5 changes: 5 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

function generateFibonacciSequence(n) {
// TODO
let fibSeq = [0, 1];
for (let i = 2; i < n; i++) {
fibSeq.push(fibSeq[i - 1] + fibSeq[i - 2]);
}
return fibSeq;
}

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