Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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);
//a isn't given a value.


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

let hello = sayHello();
console.log(hello);
//the function doesn't return anything.


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

sayHelloToUser();
//Nothing has been written inside the parantheses when calling the function.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//the array holds values from 0-2, so 3 doesn't exist.
4 changes: 2 additions & 2 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Declare some variables assigned to arrays of values
*/

let numbers = []; // add numbers from 1 to 10 into this array
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array
let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length - 1]; // complete this statement
}

/*
Expand Down
5 changes: 3 additions & 2 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
- change the first value in the array to the number 1
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration

let numbers = [1, 2, 3];
numbers.push(4);// Don't change this array literal declaration
numbers[0] = 1;
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
18 changes: 3 additions & 15 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,9 @@
Using a for loop, output to the console a line about the age of each writer.
*/

const WRITERS = [
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya"
]

const AGES = [
59,
40,
41,
63,
49
];
const WRITERS = ["Virginia Woolf", "Zadie Smith", "Jane Austen", "Bell Hooks", "Yukiko Motoya"]
const AGES = [59, 40, 41, 63, 49];
for (i = 0, i < )

// TODO - Write for loop code here

Expand Down
7 changes: 5 additions & 2 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
while(i <= BIRTHDAYS.length) {
if (array[i] === July) {
return i;
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
console.log(findFirstJulyBDay(BIRTHDAYS)); }
9 changes: 8 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
*/

function getTemperatureReport(cities) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice 👍

// TODO
const temperatureReport = [];

for (let i = 0; i < cities.length; i++) {
const temperature = temperatureService(cities[i]);
temperatureReport.push(`The temperature in ${cities[i]} is ${temperature} degrees`);
}

return temperatureReport;
}


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a good solution!
For extra practice, you could try re-writing this with the filter array method.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Trying other methods and ways of solving code is something I'm going to be aiming for. Thank you.

// TODO
}
const potentialHeadlines = [];

for (let i = 0; i < allArticleTitles.length; i++) {
const articleTitle = allArticleTitles[i];

if (articleTitle.length <= 65) {
potentialHeadlines.push(articleTitle);
}
}

return potentialHeadlines;
}

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your solution will give you the title with the fewest characters, but this might not be the fewest words.
Can you fix this by making a couple of small changes?

// TODO
let shortestTitle = allArticleTitles[0];

for (let i=1; i<allArticleTitles.length; i++) {
if (allArticleTitles[i].length < shortestTitle.length) {
shortestTitle = allArticleTitles[i];
}
}
return shortestTitle;
}

/*
Expand All @@ -23,19 +40,33 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
}
const result = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (/[\d]/.test(allArticleTitles[i]))
result.push(allArticleTitles[i]);
}
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks perfect! Good job 😄

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks!

// TODO
let totalChars = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
totalChars += allArticleTitles[i].length;
}
const averageChars = totalChars / allArticleTitles.length;
return Math.round(averageChars);
}





/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
43 changes: 39 additions & 4 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
*/


/* ======= Stock data - DO NOT MODIFY ===== */
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

Expand Down Expand Up @@ -34,9 +35,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good solution, and nicely explained 👍

// TODO
}
// Create an empty array to hold the average prices for each stock
const averagePrices = [];

// Loop through each array of closing prices for each stock
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
// Get the array of closing prices for the current stock
const stockPrices = closingPricesForAllStocks[i];

// Calculate the sum of all closing prices for the current stock
const sum = stockPrices.reduce((acc, cur) => acc + cur);

// Calculate the average closing price for the current stock
const avg = sum / stockPrices.length;

// Round the average closing price to 2 decimal places and add it to the array
averagePrices.push(parseFloat(avg.toFixed(2)));
}

return averagePrices;
}
/*
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,7 +66,15 @@ 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
const priceChanges = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
const prices = closingPricesForAllStocks[i];
const priceChange = (prices[prices.length-1] - prices[0]).toFixed(2);
priceChanges.push(parseFloat(priceChange));
}

return priceChanges;
}

/*
Expand All @@ -64,7 +90,16 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const priceDescriptions = [];

for (let i = 0; i < stocks.length; i++) {
const ticker = stocks[i].toUpperCase();
const highestPrice = Math.max(...closingPricesForAllStocks[i]).toFixed(2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice use of Math.max

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's nice finding things and trying things I'm unfamiliar with on Google.

priceDescriptions.push(`The highest price of ${ticker} in the last 5 days was ${highestPrice}`);

}

return priceDescriptions;
}


Expand Down
30 changes: 11 additions & 19 deletions 3-extra/1-radio-stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,20 @@
* Note: You are not expected to understand everything below this comment!
*/

function getAvailableStations() {
// Using `stations` as a property as defining it as a global variable wouldn't
// always make it initialized before the function is called
if (!getAvailableStations.stations) {
const stationCount = 4;
getAvailableStations.stations = [];
while (getAvailableStations.stations.length < stationCount) {
let randomFrequency = Math.floor(Math.random() * (108 - 87 + 1) + 87);
if (!getAvailableStations.stations.includes(randomFrequency)) {
getAvailableStations.stations.push(randomFrequency);
}
}
getAvailableStations.stations.sort(function (frequencyA, frequencyB) {
return frequencyA - frequencyB;
});
function getAllFrequencies() {
const frequencies = [];
for (let i = 87; i <= 108; i++) {
frequencies.push(i);
}

return getAvailableStations.stations;
return frequencies;
}

function isRadioStation(frequency) {
return getAvailableStations().includes(frequency);
function getStations() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great work 😄
A small note, but arrow functions can be simplified even further. In this case, we can write the following:

const radioStations = allFrequencies.filter(frequency => isRadioStation(frequency));

const allFrequencies = getAllFrequencies();
const radioStations = allFrequencies.filter((frequency) => {
return isRadioStation(frequency);
});
return radioStations;
}

test("getAllFrequencies() returns all frequencies between 87 and 108", () => {
Expand Down
16 changes: 14 additions & 2 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
}
// this object will store the highest rated books for each genre
const highestRated = {};

// I'll loop through the books array and update the highest rated book for each genre
books.forEach(book => {
if (!highestRated[book.genre] || book.rating > highestRated[book.genre].rating) {
highestRated[book.genre] = book;
}
});

// this will return an array of book titles from the highest rated books object
const titles = Object.values(highestRated).map(book => book.title);

return titles;
}

/* ======= Book data - DO NOT MODIFY ===== */
const BOOKS = [
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 fibonacciSequence = [0, 1];

for (let i = 2; i < n; i++) {
fibonacciSequence[i] = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
}

return fibonacciSequence;
}

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