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 @@ -10,11 +10,15 @@
*/

// Example 1
// declare the variable a but we haven’t assign yet

let a;
let a;
console.log(a);


// Example 2
// undefined is the return value of functions which don’t return a value
function sayHello() {
let message = "Hello";
}
Expand All @@ -24,6 +28,7 @@ console.log(hello);


// Example 3
// parameters for which no value is passed to the function it is undefined to.
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}
Expand All @@ -32,5 +37,6 @@ sayHelloToUser();


// Example 4
// Array elements that do not have a value to undefined
let arr = [1,2,3];
console.log(arr[3]);
15 changes: 12 additions & 3 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@

function evenNumbers(n) {
// TODO
let even=[]
let i =0

while(even.length<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.

👍

even.push(i);
i+=2;

}return even;

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 final output is correct except it should return a string. Think of a method that allows you to do that.

}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

So, technically, according to the instructions, the function should console.log things.

console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18
6 changes: 2 additions & 4 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const BIRTHDAYS = [
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
}
const findFirstJulyBDay = BIRTHDAYS.find(e=>e.includes('July'));

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 console displays an error message saying it is not a function. There might be some syntax problems here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this is a clever solution and kind of does solve the problem, you should try and implement things as asked by the exercise, rather than modify the check. So, here for example, you could do

function findFirstJulyBDay(birthdays) {
    return birthdays.find(e=>e.includes('July'));
}

To create a function.

Also, this should technically be a "while" loop exercise -- how would you complete it with a while loop?

console.log(findFirstJulyBDay);

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
12 changes: 10 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
*/

function evenNumbersSum(n) {
// TODO
}
let startNumber = 0;
let sum = 0;
do {
sum += startNumber * 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 :)

startNumber++;
} while (startNumber < n);
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.

Your code would work if you placed your changes inside the function.

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 code does work :) Maybe it looked confusing on a github diff?




console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
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++) {

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(String.fromCharCode(97 + i));
i++;

}
// The output shouldn't change.
8 changes: 8 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,14 @@ const AGES = [
];

// TODO - Write for loop code here
for (var i = 0; i < WRITERS.length; 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.

Try to use let instead of var


console.log(`${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.

Good use of template literals.

}





/*
The output should look something like this:
Expand Down
12 changes: 12 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for(let element of tubeStations){

console.log(element);

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 but get rid of the unnecessary spaces:)


}



// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for(let element of str){
const myArr=str.toUpperCase().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.

I think you should target 'element' here not 'str'. The aim of this exercise is to output each letter separately, not the whole string. Also, I don't think you need split method here because it unnecessarily converts you string back to an array.

console.log(myArr);

}

10 changes: 9 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@

function getTemperatureReport(cities) {
// TODO
let cityWithTemperature = [];
for (let i = 0; i < cities.length; i++) {
let currentCity = cities[i];
let currentCityTemperature = temperatureService(currentCity);

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 usage of variable names!

cityWithTemperature.push(
`The temperature in ${currentCity} is ${currentCityTemperature} degrees`
);
}
return cityWithTemperature;
}


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

function temperatureService(city) {
Expand Down
11 changes: 7 additions & 4 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
}

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

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

Expand Down
47 changes: 45 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter((element) => element.length <= 65);

}

/*
Expand All @@ -15,7 +17,31 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
let shortestTitleLength = Infinity;
return allArticleTitles.reduce((acc, title) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Indentation.

const titleLengthInWords = title.split(" ").length;
if (titleLengthInWords < shortestTitleLength) {
shortestTitleLength = titleLengthInWords;
return title;
} else {
return acc;
}
}, "");
}
// function headlinesWithNumbers(allArticleTitles) {
//input is an array
//only return array entries that have numbers in
//return an array
// return allArticleTitles.filter((title) => {
// const matches = title.match(/\d/);
// if (matches !== null) {
// return true;
// } else {
// return false;
// }
// });

// }

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Expand All @@ -24,14 +50,31 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
}
const titlesToReturn = [];
const numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
allArticleTitles.forEach((title) => {
title.split("").forEach((character) => {
if (numbers.includes(character)) {
titlesToReturn.push(title);
}
});
});
return titlesToReturn;
}


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

.length works on strings, too:

> str = "somestr"
'somestr'
> str.length
7

}
return Math.round(totalCharacterNumber / allArticleTitles.length);
}


Expand Down
33 changes: 32 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
}

return closingPricesForAllStocks.map((value, index) => {
let sum = 0, counter = 0;

for (let item of value) {
sum += item;
counter++;
}
average = parseFloat((sum/counter).toFixed(2))

console.log(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.

You should try and remove all console.log you used for debugging before your final submission.

return average;
})
}



/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -49,6 +64,14 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let priceDiffArr=[];

for(let price of closingPricesForAllStocks){

let priceDiff=Number(price[price.length-1]-price[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.

Is Number needed here?

priceDiffArr.push(parseFloat(priceDiff.toFixed(2)))
}
return priceDiffArr;
}

/*
Expand All @@ -65,6 +88,14 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPriceByCompany = [];
for( let i = 0; i < closingPricesForAllStocks.length; i++ ){
let highestPrice = Math.max(...closingPricesForAllStocks[i]).toFixed(2);
highestPriceByCompany.push(
`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice}`
);
}
return highestPriceByCompany;
}


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

function factorial(input) {
// TODO
for (let i = input-1; i >=1; i--){
input = input* i;
console.log(input)
}
return input;

}



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

test("3! should be 6", () => {
Expand Down
10 changes: 10 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

function getHighestRatedInEachGenre(books) {
// TODO
let bookTitles=[];
for(let i=0; i<books.length; i++){
if(books[i].rating > 4.8){

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 for the exact test case provided, but wouldn't work with different inputs (for instance, if a genre's highest-rated book was rated 4.5), or there were two books rated > 4.8 in one genre.

bookTitles.push(books[i].title)
}

}
return bookTitles;


}


Expand Down
11 changes: 11 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@

function generateFibonacciSequence(n) {
// TODO

let fibonacci = [0, 1];
for(i = 2; i < n; 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.

Great work on this one!

let y = fibonacci[i-2] + fibonacci[i-1];
fibonacci.push(y);
}


return fibonacci;
}



/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(10)).toEqual(
Expand Down