Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
8 changes: 4 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

// Example 1
let a;
console.log(a);
console.log(a); //Variable a didnt get a value assigned


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; //The function doesnt return anything
}

let hello = sayHello();
Expand All @@ -28,9 +28,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); //There is no string the function could take to fill the "user"


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); //The last array position is 2 not 3
20 changes: 18 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/

function evenNumbers(n) {
// TODO
function evenNumbers(n)
{
let total = "0";
let calc = 0;

if (n === 0)
{
return console.log("");
}

for(let i = 1; i < n; i++)

@Alex-Phillip Alex-Phillip Aug 19, 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.

Hi Kerim, I've looked through your code and it's great! One suggestion I have here however is using a while loop instead of a for loop.
Great work!

{
calc = calc + 2;

total = total + ", " + calc;
}

return console.log(total);
Comment on lines +8 to +25

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 instructions said that you should use a while loop.
Consider using an array to store the values and then convert it to string using 'toString' method

}

evenNumbers(3); // should output 0,2,4
Expand Down
15 changes: 13 additions & 2 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,19 @@ const BIRTHDAYS = [
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
function findFirstJulyBDay(birthdays)
{
let Arrlength = birthdays.length;
i = 0;

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

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
17 changes: 14 additions & 3 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

function evenNumbersSum(n) {
// TODO
function evenNumbersSum(n)
{
total = 0;
i = 0;

do
{
total += i * 2
i++;
}
while ( i < n );

return total;
}

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
console.log(evenNumbersSum(10)); // should output 90
5 changes: 2 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,8 @@


// 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++)
{
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] + " 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.

Suggested change
console.log(WRITERS[i] + " is " + AGES[i] + " old")
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 (i of tubeStations)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
for (i of tubeStations)
for (const station of tubeStations)

I think it will be better to use a meaningful variables names, it's easy to read and understand ("i" generally refers to index which is not the case here)

{
console.log(i);
}

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

for (i of str)
{
console.log(i.toUpperCase())
}
13 changes: 10 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO
function getTemperatureReport(cities)

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 work!

{
let TemperatureArray = [];

for (let city of cities)
{
TemperatureArray.push("The temperature in " + city + " is " + temperatureService(city) + " degrees");
}
return TemperatureArray;
}


Expand Down Expand Up @@ -60,4 +67,4 @@ test("should return a temperature report for the user's cities (alternate input)

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
});
});
12 changes: 10 additions & 2 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ function generateRandomNumber() {
return Math.round(Math.random() * 100);
}

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

return total;
Comment on lines +12 to +21

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 the use of 'total' as the name of the variable creates a little confusion as we don't really calculate any total in this function

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
52 changes: 42 additions & 10 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
/*
Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array.

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

function potentialHeadlines(allArticleTitles)
{
let AcceptedArticles = []

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

/*
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

function titleWithFewestWords(allArticleTitles)
{
const shorter = (left, right) => left.length <= right.length ? left : right;

return allArticleTitles.reduce(shorter);
}

Comment on lines +27 to 33

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you please add some comments to this function, I didn't really understand it?

/*
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

function headlinesWithNumbers(allArticleTitles)
{
let AcceptedArticles = [];

for (let i = 0; i < allArticleTitles.length; i++)
{
if (/\d/.test(allArticleTitles[i]))
{
AcceptedArticles.push(allArticleTitles[i]);
}
}
return AcceptedArticles;
}

/*
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
}

function averageNumberOfCharacters(allArticleTitles)
{
let AllChar = 0;
let Average = 0;
for (let i = 0; i < allArticleTitles.length; i++)
{
AllChar += (allArticleTitles[i].length);
}

return Math.round(Average = AllChar / allArticleTitles.length);
}

/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
Expand Down
39 changes: 32 additions & 7 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
//https://jrsinclair.com/articles/2019/five-ways-to-average-with-js-reduce/
function getAveragePrices(closingPricesForAllStocks)
{
//const reduce = r => i => a => a.reduce(r, i);
//conatiner = getting items => got items.reduce => add / length .map previous => round to 2 dec
const average = (closingPricesForAllStocks.map((array) => array.reduce((r, current) => r + current / array.length, 0))).map((element) => parseFloat(element.toFixed(2)));

return average;
Comment on lines +36 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you please delete unnecessary comments to make the code cleaner :D

}

/*
Expand All @@ -47,8 +53,12 @@ function getAveragePrices(closingPricesForAllStocks) {
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

function getPriceChanges(closingPricesForAllStocks)
{
const PriceChange = (closingPricesForAllStocks.map((array) => array[array.length - 1] - array[0])).map((element) => parseFloat(element.toFixed(2)));

return PriceChange;
Comment on lines +57 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
function getPriceChanges(closingPricesForAllStocks)
{
const PriceChange = (closingPricesForAllStocks.map((array) => array[array.length - 1] - array[0])).map((element) => parseFloat(element.toFixed(2)));
return PriceChange;
function getPriceChanges(closingPricesForAllStocks)
{
const priceChange = closingPricesForAllStocks.map((array) => array[array.length - 1] - array[0])
const result = priceChange.map((element) => parseFloat(element.toFixed(2));
return result;

It's better to use to variables to make code more readable

}

/*
Expand All @@ -63,11 +73,26 @@ function getPriceChanges(closingPricesForAllStocks) {
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}

function highestPriceDescriptions(closingPricesForAllStocks, stocks)
{
let HighestPriceText = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++)
{
let AllHighestPrices = 0;
for(let i2 = 0; i2 < closingPricesForAllStocks[i].length; i2++)
{
if(closingPricesForAllStocks[i][i2] > AllHighestPrices)
{
AllHighestPrices = closingPricesForAllStocks[i][i2];
}
}
HighestPriceText.push("The highest price of " + STOCKS[i].toUpperCase() + " in the last 5 days was " + AllHighestPrices.toFixed(2));
}

return HighestPriceText;
}
/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand Down
15 changes: 13 additions & 2 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@
Using a loop, complete the function below so it returns the factorial of the number being passed in.
*/

function factorial(input) {
// TODO
function factorial(input)
{
let OriginalInput = input;
let Total = 1;
for (let i = 0; i < input; i++)
{
if(input > 0)
{
Total *= OriginalInput;
OriginalInput--;
}
}
return Total;
}

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