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

// Example 1
let a;
console.log(a);
console.log(a); // a has been declared but not initialized, as it does not have a value, it returns undefined.


// Example 2
function sayHello() {
let message = "Hello";
}
}

let hello = sayHello();
console.log(hello);
// the function does not return anything


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

sayHelloToUser();
//when the function is called, there is no value given for the user argument, this results in the user variable being undefined


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//arrays are zero indexed so the highest given index in this is 2 so there is no value in arr[3]
10 changes: 9 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
*/

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

evenNumbers(3); // should output 0,2,4
Expand Down
10 changes: 9 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,15 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let julyDates = [];
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
julyDates.push(birthdays[i]);
}
i++;
}
return julyDates[0];
}

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

function evenNumbersSum(n) {
// TODO
let numbers = [];
let sum = 0;
let i = 0;
do {
if (i % 2 === 0) {
sum += i;
numbers.push(i);
}
i++;
} while (numbers.length < n);
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
4 changes: 1 addition & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


// 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.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const AGES = [
49
];

// TODO - Write for loop code here
for (var 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 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 letter of str) {
console.log(letter.toUpperCase())
}
6 changes: 5 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/

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


Expand Down
8 changes: 7 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,13 @@ function generateRandomNumber() {
}

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
37 changes: 33 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let allowedTitles = [];
for (let title of allArticleTitles) {
if (title.length <= 65) {
allowedTitles.push(title);
}
}
return allowedTitles;
}

/*
Expand All @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let allTitles = potentialHeadlines(allArticleTitles);
let wordsLength = Infinity;
let titleWords = "";
for (let title of allTitles) {
let words = title.split(" ");
if (words.length < wordsLength) {
wordsLength = words.length;
titleWords = title;
}
}
return titleWords;
}

/*
Expand All @@ -23,15 +39,28 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let hasNumber = [];
for (title of allArticleTitles) {
if (title.match(/[0-9]/g)) {
hasNumber.push(title);
}
}
return hasNumber;
}

/*
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 titles = [];
for (title of allArticleTitles) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you want a little extension you can try writing this function without the for loop step. Tip: have a think about how you can do it with just the reduce function below!

titles.push(title.length);
}
let total = titles.reduce((a, b) => {

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! I'd recommend avoiding very short variable names in a reduce because it can make it hard to know what they refer to.

Also does reduce make sense to you? Can you think of other situations where reduce is helpful?

return a + b;
})
return parseInt(total / titles.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.

Why do you need parseInt here?

}


Expand Down
61 changes: 42 additions & 19 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let closingPrices = closingPricesForAllStocks;
let averagePrices = [];
for (let prices of closingPrices) {
let total = prices.reduce((a, b) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above about short variable names

return a + b;
});
averagePrices.push(parseFloat((total / prices.length).toFixed(2)));
}
return averagePrices;
}

/*
Expand All @@ -47,8 +55,17 @@ 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 rounded(num) {

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 👍

return parseFloat(num.toFixed(2));
}
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let closingPrices = closingPricesForAllStocks;
let priceChange = [];
for (let prices of closingPrices) {
let change = prices[prices.length - 1] - prices[0];
priceChange.push(rounded(change));
}
return priceChange;
}

/*
Expand All @@ -64,31 +81,37 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}
let closingPrices = closingPricesForAllStocks;
let stock = stocks;

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's this for?

let highestArr = [];

for (let i = 0; i < closingPrices.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.

This looks good. Can you think of another way how you could find the highest without a for loop? (tip: what if you could easily get the highest element and lowest element)

let highest = -Infinity;
for (let j = 0; j < closingPrices[i].length; j++) {
if (closingPrices[i][j] > highest) {
highest = closingPrices[i][j];
}
}
highestArr.push(`The highest price of ${stock[i].toUpperCase()} in the last 5 days was ${highest.toFixed(2)}`);
}
return highestArr;
}

/* ======= 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",
]);
});
11 changes: 10 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
*/

function factorial(input) {
// TODO
let num = [];
let i = 1;
while (i <= input) {
num.push(i);
i++;
}
let result = num.reduce((a, b) => {
return a * b;
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a more concise way to do this. (Tip: how could you do this without the reduce and only using the while loop above?)

return result;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
15 changes: 14 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
let sorted = books.sort((a, b) => {
return a.rating === b.rating ? 0 : a.rating < b.rating ? 1 : -1;
});
let i = 0;
let genres = [];
let topTitles = [];
while (genres.length < 3) {
if (!genres.includes(sorted[i].genre)) {
genres.push(sorted[i].genre);
topTitles.push(sorted[i].title);
}
i++;
}
return topTitles;
}


Expand Down
8 changes: 7 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
*/

function generateFibonacciSequence(n) {
// TODO
let sequence = [0, 1];
let i = 1;
while (sequence.length < n) {
sequence.push(sequence[i] + sequence[i - 1]);
i++;
}
return sequence;
}

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