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
12 changes: 8 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1
// Example 1 // console.log is returning undefined due to the "a" variable has not been assigned a value

let a;
console.log(a);


// Example 2
// Example 2 // console.log is returning undefined as "hello" has been assigned to the function sayHello() which does not have a return value
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +24,17 @@ let hello = sayHello();
console.log(hello);


// Example 3
// Example 3 //console.log is returning undefined as the parameter "user" has not been assigned a value

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

sayHelloToUser();


// Example 4
// Example 4 // console.log is returning undefined as in JavaScript, arrays are zero-indexed with the first element of an array being 0,
// the second element being 1 etc. In this example, there are three elements, however, the index ends at 2. Therefore, arr[3] returns
// undefined as the index ends at 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 explanations

let arr = [1,2,3];
console.log(arr[3]);
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 @@ -6,7 +6,16 @@
*/

function evenNumbers(n) {
// TODO
let evenArr = [];
let index = 0;
while (n > evenArr.length) {
if (index % 2 === 0) {
evenArr.push(index);
}
index ++;
//console.log(evenArr);
}
console.log(evenArr);
}

evenNumbers(3); // should output 0,2,4
Expand Down
9 changes: 8 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,14 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let index = 0;

while (index < birthdays.length) {
if (birthdays[index].includes("July")) {
return birthdays[index];
}
index ++;
}
}

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!


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

function evenNumbersSum(n) {
// TODO
let sum = 0;
let index = 0;

do {
sum += index * 2;
index ++;
} while (index < n);
return sum;
}

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


// 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.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const AGES = [
49
];

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

/*
The output should look something like this:
Expand Down
6 changes: 6 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,12 @@ let tubeStations = [
"Tottenham Court Road"
];

for (let busStop of tubeStations) {
console.log(busStop);
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (let char of str) {
console.log(char.toUpperCase());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

well done Franklin

7 changes: 6 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/

function getTemperatureReport(cities) {
// TODO
let arr = [];
for (const element of cities) {
if (temperatureService(element)) {
arr.push(`The temperature in ${element} is ${temperatureService(element)} degrees`);
}
} return arr;
}


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

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let index = 0;
let arr = [];
do {
arr.push(generateRandomNumber());
if (arr[index] > 50) {
arr = arr[index];
}
index ++;
}
while (index <= arr.length);
return arr
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
35 changes: 29 additions & 6 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
}
let arr = [];
for (const element of allArticleTitles) {
if (element.length <= 65) {
arr.push(element);
}
}return arr;

/*
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
const sortedArr = allArticleTitles.sort((a,b) => {
// console.log(a.split(" ").length);
return a.split(" ").length - b.split(" ").length;
});
return sortedArr[0];
}
}

/*
Expand All @@ -23,16 +32,30 @@ 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 array = []
for (const element of allArticleTitles){
if ( /\d/.test(element)) {
array.push(element);
}
} return array;
}

/*
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 arrNumOfCharacters = [];
let total = 0;
let average = 0;
for (const element of allArticleTitles) {
arrNumOfCharacters.push(element.trim().length);
}
for (const element of arrNumOfCharacters) {
total = total + element;
}
average = total / arrNumOfCharacters.length;
return Math.round(average);



Expand Down
12 changes: 9 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
const average = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.map(prices => prices.reduce((a, b) => a + b) / prices.length);
// console.log(average)
return average.map(value => Number(value.toFixed(2)))
}

/*
Expand All @@ -48,7 +50,8 @@ 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
const priceChange = closingPricesForAllStocks.map(prices => prices[prices.length - 1] - prices[0]);
return priceChange.map((value) => Number(value.toFixed(2)));
}

/*
Expand All @@ -64,7 +67,10 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const sortedArr = closingPricesForAllStocks.map(prices => prices.sort((a, b) => b - a));
return sortedArr.map((prices, index) => {
return `The highest price of ${STOCKS[index].toUpperCase()} in the last 5 days was ${prices[0].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.

Good job!



Expand Down
9 changes: 6 additions & 3 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
Using a loop, complete the function below so it returns the factorial of the number being passed in.
*/

function factorial(input) {
// TODO
}
function factorial(sum) {
let count = 1;
for (let index = sum; index >= 1; index --) {
count *= index;
}
return count }

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

Expand Down
17 changes: 16 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
let genreList = ["non-fiction", "children", "cooking"];
let maxRatingTitles = [];
for (const genre of genreList) {
let maxRating = 0;
let maxIndex = 0;
for (let i = 0; i < books.length; i++) {
if (books[i].genre === genre) {
if (books[i].rating > maxRating) {
maxRating = books[i].rating;
maxIndex = i;
}
}
}
maxRatingTitles.push(books[maxIndex].title);
}
return maxRatingTitles;
}


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

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

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