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

//A variable was declared but no value was assigned to it.

// Example 2
function sayHello() {
let message = "Hello";
}
//when a function does not return anything

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

// executing a function that produces no output


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

sayHelloToUser();

// A function that is declared with a parameter but receives no arguments

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

// The index does not exist in the array.
19 changes: 17 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,24 @@
*/

function evenNumbers(n) {
// TODO
}

let arr = [];
let i = 0;
while (n > arr.length) {
if (i % 2 === 0) {
arr.push(i);
}
i++;
}

console.log(arr.toString());
}



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



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 i = 0;
while (i < birthdays.length) {
let month = birthdays[i].substr(0, 3);
if (month === "Jul") {
return birthdays[i];
}
i++;
}
}

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

function evenNumbersSum(n) {
// TODO
let i = 0;
const evenNum = [];
do {
if (i % 2 === 0) {
evenNum.push(i);
}
i++
} while (n > evenNum.length)

let sum = evenNum.reduce((acc, curr) => acc + curr, 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.

I like that you used reduce method here

return sum;

}

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


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
// 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));
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 (let i = 0; i < WRITERS.length; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}

/*
The output should look something like this:

Expand Down
7 changes: 6 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it would be nice to declare variable - arr

console.log(arr.toUpperCase())
}
8 changes: 7 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
*/

function getTemperatureReport(cities) {
// TODO
const temp = [];
for (let i = 0; i < cities.length; i++) {
temp.push('The temperature in ' + cities[i] + ' is ' + temperatureService(cities[i]) + ' degrees');
}
return temp;
}

// temp.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`)


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

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
48 changes: 45 additions & 3 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,76 @@
The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/

function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(title => title.length <= 65)

@jdbevan jdbevan Aug 30, 2022

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 use of .filter(). Why did you chose this solution vs the commented out one?


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


/*
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 array = allArticleTitles[0].split(" ").length;

@jdbevan jdbevan Aug 30, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What do you think will happen on this line of code if there are no articles?

Naming is hard. What data does the variable array contain? Can you think of a better variable name?

let spaceCount;
for (let i = 0; i < allArticleTitles.length; i++) {
wordCount = 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.

I suggest to add .trim() method before split ,Because if there is any space at first or in the end of string it will wrongly add to the number of words

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is missing from this line?

if (wordCount < array) {
array = wordCount;
spaceCount = i;
}
}
return allArticleTitles[spaceCount];
}


/*
The editor of the FT has realised 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
return allArticleTitles.filter(headline => /[0-9]/.test(headline));

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 like this solution

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do you like it? What is appealing about this code?

@simeonbikov simeonbikov Aug 30, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My comment is a sample of how beginners comment who make a review for beginners))
I liked that it fits in one line and it is clear what is going on.


// let result = [];
// for (let i = 0; i < allHeadLineTitles.length; i++) {
// let innersentece = allArticleTitles[i]
// for (let j = 0; j < innersentece.length; j++){
// let value = innersentece[j]
// if (!isNaN(value) && value != " ")
// {
// result.push(innersentece)
// break;
// }
// }
// }
// return result;
}

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


Expand Down
86 changes: 62 additions & 24 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.2, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL
[1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA
];

/*
Expand All @@ -34,7 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let arr = [];
let average = 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.

What is the benefit of defining this variable outside the for loop?

for (const stockPrices of closingPricesForAllStocks) {
let stockSum = 0;
for (const price of stockPrices) {
stockSum += price;
}
average = parseFloat((stockSum / stockPrices.length).toFixed(2));
arr.push(average);
}
return arr;
}

/*
Expand All @@ -48,7 +58,14 @@ 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 changeArray = [];
for (const stockPrices of closingPricesForAllStocks) {
let priceChange = parseFloat(
(stockPrices[stockPrices.length - 1] - stockPrices[0]).toFixed(2)
);
changeArray.push(priceChange);
}
return changeArray;
}

/*
Expand All @@ -64,31 +81,52 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const sortedPrices = closingPricesForAllStocks.map((prices) =>
prices.sort((a, b) => b - a)
);
return sortedPrices.map((price, index) => {

@jdbevan jdbevan Aug 30, 2022

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 use of .map() 😄 why did you go with this vs the commented out code?

return `The highest price of ${stocks[
index
].toUpperCase()} in the last 5 days was ${price[0].toFixed(2)}`;
});
// let result = []
// for (let i = 0; i < closingPricesForAllStocks.length; i++) {
// let companyStock = closingPricesForAllStocks[i];
// let highestValue = companyStock[0];
// for (let j = 0; j < companyStock.length; j++) {
// if (companyStock[j] > highestValue) {
// highestValue = companyStock[j]
// }
// }
// result[i] = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2)
// }
// return result;
}

// console.log(
// highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)
// );

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[176.89, 335.66, 3405.66, 2929.22, 1041.93]
);
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
176.89, 335.66, 3405.66, 2929.22, 1041.93,
]);
});

test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
-6.2, -13.4, 23.9, -82.43, -162.77,
]);
});

test("should return a description of the highest price for each stock", () => {
expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual(
[
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
expect(
highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)
).toEqual([
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30",
]);
});
15 changes: 14 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
*/

function factorial(input) {
// TODO
let product = 1;
for (let i = input; i > 0; i--){
product = product * i;
}
return product;

// Alternate Solution
// var f = [];
// if (input == 0 || input == 1)
// return 1;
// if (f[input] > 0)
// return f[input];
// return f[input] = factorial(input-1) * input;
}


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

test("3! should be 6", () => {
Expand Down
Loading