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
10 changes: 5 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
*/

// Example 1
let a;
let a; // variable has no value assigned
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; // no return value inside the function
}

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


// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
console.log(`Hello ${user}`); // no argument
}

sayHelloToUser();


// Example 4
let arr = [1,2,3];
let arr = [1,2,3]; // index [3] not assigned the value in array
console.log(arr[3]);
12 changes: 12 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@

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

console.log(array);
}



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
7 changes: 7 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO

let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) return birthdays[i];
i++;
}
}


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

function evenNumbersSum(n) {
// TODO
}
let sum = 0;
let i = 0;
do {
sum += i * 2;
i++;
} while (i < n);

return sum
}





console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
Expand Down
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: 5 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ 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
5 changes: 5 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,12 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for(let station of tubeStations){
console.log(station);}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (let letters of str){
console.log(letters.toUpperCase());
}
14 changes: 14 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How could you rewrite this without a for loop? (Hint: .map() array method)

let temp = temperatureService(cities[i]);
console.log(temp);
tempArray[i] = "The temperature in " + cities[i] + " is " + temp + " degrees";
}
return tempArray
}









/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
13 changes: 13 additions & 0 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let randomNumber = 0;

do {
randomNumber = generateRandomNumber();
}
while (randomNumber <= 50);

return randomNumber;

}





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

test("Returned value should always be greater than 50", () => {
Expand Down
31 changes: 31 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
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 arr = allArticleTitles;
let newArr = [...allArticleTitles].sort((a, b) => a.length - b.length);
return newArr[0];
}

/*
Expand All @@ -24,14 +37,32 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let newArray = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (/\d/.test(allArticleTitles[i])) {
newArray.push(allArticleTitles[i]);
}
}

return newArray;
}




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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What happens here is allARticleTitltes is an empty array?


}


Expand Down
31 changes: 30 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averageStock = [];
let sum = 0;
for (let stockPriceBrand of closingPricesForAllStocks) {
for (let unitStockPriceBrand of stockPriceBrand) {
sum += unitStockPriceBrand;
}
let averageAllStock = sum / 5;
averageStock.push(parseFloat(averageAllStock.toFixed(2)));
sum = 0;
}
return averageStock;
}

/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
Expand All @@ -49,8 +60,17 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let priceChangeStock = [];
for (let stockPriceBrand of closingPricesForAllStocks) {
let priceChangeBrand = stockPriceBrand[4] - stockPriceBrand[0];
let FormattedPriceChangeBrand = parseFloat(priceChangeBrand.toFixed(2));
Comment on lines +65 to +66

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

try to use const where possible - also in general functions and variables should be lowerCamelCase, UpperCaselCase (also called PascalCase) is reserved for classes and types

priceChangeStock.push(FormattedPriceChangeBrand);
priceChangeBrand = 0;
}
return priceChangeStock;
}


/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
Expand All @@ -65,6 +85,15 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highPrice=[];

stocks.forEach((stock,item) => {
highPrice.push(
`The highest price of ${stock.toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[item]).toFixed(2)}`
);
});
return highPrice;

}


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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good, how could you rewrite this without a for loop? (https://www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/)

input = input * i;
}
return input;
}
}

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

test("3! should be 6", () => {
expect(factorial(3)).toEqual(6);
});
Expand All @@ -25,3 +32,4 @@ test("5! should be 120", () => {
test("10! should be 3628800", () => {
expect(factorial(10)).toEqual(3628800);
});

25 changes: 25 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,34 @@

function getHighestRatedInEachGenre(books) {
// TODO
let resultBooks = {};
let genres = [];
let result = [];

for (let i = 0; i < books.length; i++) {
let genre = books[i]["genre"];
if (genres.indexOf(genre) < 0) {
genres.push(genre);
}

if (
!resultBooks[genre] ||
!resultBooks[genre]["rating"] ||
resultBooks[genre]["rating"] < books[i]["rating"]
) {
resultBooks[genre] = books[i];
}
}
for (let i = 0; i < genres.length; i++) {
result.push(resultBooks[genres[i]]["title"]);
}

return result;

}



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

function generateFibonacciSequence(n) {
// TODO
let fib = [0, 1];
let data = [];

for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
data.push(fib[i]);
}

return fib;

}

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