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
4 changes: 4 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Example 1
let a;
console.log(a);
we did not define a.


// Example 2
Expand All @@ -21,6 +22,7 @@ function sayHello() {

let hello = sayHello();
console.log(hello);
we need to call hello () not just hello


// Example 3
Expand All @@ -29,8 +31,10 @@ function sayHelloToUser(user) {
}

sayHelloToUser();
there is no variable and definition Here.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
there is nothing for [3] it is empty
9 changes: 8 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;

while (i<=n){
if(i%2==0)
i++;

}
}
Comment on lines +11 to 16

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.

Right idea, but you've created an infinite loop here (while i<=n but you only increment i if i%2===0, so when it reaches i=1 you infinitely loop.

What you need is two variables; one to increment on every iteration up to n, and one hold the even numbers

const evenNums = []
let i = 0
while (evenNums.length < n) {
   if(i % 2 === 0) {
      evenNums.push(i)
   }
   i++
}
return evenNums

console.log(evenNumbers());

evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
Expand Down
7 changes: 6 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) return birthdays[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.

is all good, but try to make sure to include {} for if statements, even if they're one line (keeps things consistent)

i++;
}
}

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

function evenNumbersSum(n) {
// TODO
}
let counter = 1;
let sum = 0;

do {
if (counter % 2 === 0) {
sum = sum + counter;

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.

this can be cleaned up by using the += operator; sum += counter

}
counter ++;
} while (counter <= n){

return sum;
}}

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


// 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));
}
// The output shouldn't change.
7 changes: 7 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,13 @@ 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`))
}

// (WRITERS,AGES).forEach((WRITER, AGE) => console .log (`${WRITER } is ${AGE} 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 tubeStation of tubeStations){
console.log(tubeStation.toUpperCase(""));
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (let tube of str){
console.log(tube.toUpperCase());
}
71 changes: 35 additions & 36 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,51 @@
*/

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


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

function temperatureService(city) {
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
return temparatureMap.get(city);
let temparatureMap = new Map();

temparatureMap.set("London", 10);
temparatureMap.set("Paris", 12);
temparatureMap.set("Barcelona", 17);
temparatureMap.set("Dubai", 27);
temparatureMap.set("Mumbai", 29);
temparatureMap.set("São Paulo", 23);
temparatureMap.set("Lagos", 33);

return temparatureMap.get(city);
Comment on lines -22 to +38

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.

think your formatter settings conflict with the repo here ; is not a huge deal but something to watch out for

}

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees"
]);
let usersCities = ["London", "Paris", "São Paulo"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees",
]);
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
"Dubai"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees"
]);
let usersCities = ["Barcelona", "Dubai"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees",
]);
});

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

// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
console.log("Generating number...");
return Math.round(Math.random() * 100);

}

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
16 changes: 16 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {

return allArticleTitles.filter(items=> items.length < 65);

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.

nice use of an arrow function! although to be a bit cleaner the items parameter should probable be called title instead

// TODO
}

Expand All @@ -14,6 +16,11 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
let title = [];
for (let i = 0; i < allArticleTitles.length; i++) {
title.push(allArticleTitles [i].split (' ').length);
}
return allArticleTitles[title.indexOf(Math.min(...title))];
// TODO
}

Expand All @@ -24,13 +31,22 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter((element) =>
[...element].find((number) => number >= "0" && number <= "9")
);
}


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

Expand Down
39 changes: 38 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,28 @@ 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 calculateAverage(newPrice) {
let sum = 0;
for (let price of newPrice) {
sum += price;
}
const average = sum / newPrice.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 if newPrice is an empty array?

return average;
}
function getAveragePrices(closingPricesForAllStocks) {
// TODO
// TODO
const myPrice = [];
for (let stock of closingPricesForAllStocks) {
const average = calculateAverage(stock);
const formattedAverage = Number(average.toFixed(2));
myPrice.push(formattedAverage);
}
return myPrice;
}

// let stocks = ["aapl", "msft", "amzn", "googl", "tsla"];
// showStocks(stocks);
// changeInPrices(closingPricesLast5Days);
/*
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 @@ -48,6 +66,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) {
let myPrice1 = [];
for (let price of closingPricesForAllStocks) {
let firstDay = price[0];
let lastDay = price[price.length - 1];
let difference = lastDay - firstDay;
myPrice1.push(Number(difference.toFixed(2)));
}
return myPrice1;
// TODO
}

Expand All @@ -64,6 +90,17 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
let newPrice = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
newPrice.push(
`The highest price of ${stocks[
i
].toUpperCase()} in the last 5 days was ${Math.max(
...closingPricesForAllStocks[i]
).toFixed(2)}`
);
}
return newPrice;
// TODO
}

Expand Down
5 changes: 5 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/

function factorial(input) {
let sum = 1;
for (i = 1; i <= input; i++) {
sum *= i;
}
return sum;
// TODO
Comment on lines 11 to 17

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.

good to see you doing the extra ones! :)

}

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

function getHighestRatedInEachGenre(books) {
const result = books.reduce((acc, cur) => {
const groupByGenre = cur.genre;
if (!acc[groupByGenre]) {
acc[groupByGenre] = [];
}
acc[groupByGenre].push(cur);
return acc;
}, {});
const genreName = Object.keys(result);
const arr = [];
for (let i = 0; i < genreName.length; i++) {
arr.push(
result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title
);
}
return arr;
// TODO
}

Expand Down
Loading