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
9 changes: 8 additions & 1 deletion 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@
let a;
console.log(a);

// There is no values assigned. Therefore undefined


// Example 2
function sayHello() {
let message = "Hello";
// there is no return to call the function. Therefore undefined
}

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



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

sayHelloToUser();

// There is no data in th brackets. Therefore undefined

// Example 4
let arr = [1,2,3];
console.log(arr[3]);

//this array contain 3 items but the index start from 0 and will display until index[2]=3
console.log(arr[2]);
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,9 +4,9 @@
Declare some variables assigned to arrays of values
*/

let numbers = []; // add numbers from 1 to 10 into this array
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // 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 mentors = ["Daniel", "Irina", "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]; //if you assume it is a number array counting start at 0
}

function last(arr) {
return; // complete this statement
return arr[arr.lenght-1]; //you have an array and want the value of its array length
}

/*
Expand Down
2 changes: 2 additions & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration
numbers.push("4");
// https://www.w3schools.com/jsref/jsref_push.asp

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
5 changes: 5 additions & 0 deletions 1-exercises/D-for-loop/exercise.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
9 changes: 9 additions & 0 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const BIRTHDAYS = [

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

// https://www.w3schools.com/jsref/jsref_startswith.asp

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
17 changes: 16 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@

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.

Looks great!
A small point - keep an eye on indentation, as it will make it easier for other developers to read your code.

// 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;
}
// I start with a function getTemperatureReport.
// It has an array cities as parameter. Then a const variable equals to an empty array. A for loop is used to iterate
// over each city in the cities array. During each iteration, the temperatureService function is called with the current
// city as its argument to get the current temperature of that city. This temperature is then stored in a temperature
// variable. Finally, a string is constructed using template literals and the current city and temperature, and this string
// is added to the temperatureReport array using the push() method. After all cities have been processed, the temperatureReport array is returned by the function.

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

Expand Down
63 changes: 59 additions & 4 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,92 @@
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) {

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!
For extra practice, can you re-write this with the filter array method?

// TODO
const headlines = [];
for (let articleTitle of allArticleTitles) {
if (articleTitle.length <= 65) {
headlines.push(articleTitle);
}
}
return headlines;
}

//I start with the function potentailHeadlines with an array argument. Inside the brakcets I have a local variable headlines
// that equals and empty array. It is used to the for ... if loop. It checks one article of all articles you see declared in
// the function. Then i have brackets with if and check the articles titles length is 65 or less. Next the .push method
// push the headlines. Then I use return the headlines.


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

This solution will return the title with the fewest characters, but this might be different from the title with the fewest words.
Can you try fixing this? (you only need to make 2 small changes 😄)

// TODO
let shortestHeadlines = allArticleTitles[0];
// console.log(allArticleTitles[0])
for (let i = 1; i < allArticleTitles.length; i++) {
let currentTitle = allArticleTitles[i];
if (currentTitle.length < shortestHeadlines.length) {
shortestHeadlines = currentTitle
}
}
return shortestHeadlines;
}

// I start with a function that want to find a title with fewest words. I have an array as a parameter. Inside the brackets
// I use a let variable shortestHeadlines that equals allArticleTitles with counting from zero. I use a for... if loop with a
// let variable i equals 1, if i is less than the length of allArticleTitles, i count 1. Then I have a let variable currentTitle
// that equals allArticleTitles and the if statement that check if length of the currentTitle to shortestHeadlines then
// shortestHeadlines equals currentTitles. Then I return the variable for the function.
/*
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
}
let newArr = [];
for (let title of allArticleTitles) {
if (/[0-9]/.test(title) === true) {

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.

You can replace this line with if (/[0-9]/.test(title)) {
Can you think of why that is the case?

newArr.push(title);
}
}

return newArr;
}
// I start with a function headlineWithNumbers with an array checking all article titles. Inside the brackets I have a let variable
// with a new array to store article titles that contain at least a number. Then I use a for ... if loop that have let variable to
// I use .test method to check for digits from 0 - 9. If there are at least a digit it return true and a title is added to newArr and
// push to containing all articles with at least a number.
//
/*
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.
*/



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 nice solution.
Can you think of another way to get the number of articles, without using the numberOfArticles variable?

function averageNumberOfCharacters(allArticleTitles) {
// TODO
let averageCharacter = 0;
let numberOfArticles = 0;
for (let articleTitle of allArticleTitles) {
averageCharacter += articleTitle.length;
numberOfArticles += 1;
}
return Math.round(averageCharacter / numberOfArticles);
}


// I start with a function that look for average number of characters with the array allArticleTitles as arguments. I use two let
// variables and both equals 0. I use a for loop with a variable that check article title of all article titles. Inside the innerbracket
// I check if average character is article title with the length method. Then number of articles += 1. I return with .round method
// and divide the two let variables averageCharacters and numberOfArticles.

/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
Expand Down
55 changes: 52 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,34 @@ 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!
*/

//

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 great solution! 👍
I love the use of the extra function here 😄 It makes the code very easy to read and understand.

function getAveragePrices(closingPricesForAllStocks) {
// TODO

let arrayWithAveragePrices = [];

for (let arrayWithPrices of closingPricesForAllStocks) {
arrayWithAveragePrices.push(findAveragePrice(arrayWithPrices));
}
return arrayWithAveragePrices;
}

function findAveragePrice(array) {
let sum = 0;
for (let price of array) {
sum += price;
}
return Number((sum / array.length).toFixed(2));
}
// I start with a function getAveragePrice and use an array as a parameter. Inside the brackets, I have a let variable arrayWithAveragePrices
// that equals to an empty array that we fill later in the code. I use a for loop and in the lopp I have a let variable with two
// parameters arrayWithPrices / closingPricesForStocks. Inside the second bracket, I have arrayWithAveragePrices with a push method
// I findAveragePrices from arrayWithPrices. Then I return the function and it match let variable arrayWithAveragePrices.

// I start with a function findAveragePrice and have array as parameter. Inside the brackets I use a let variable sum equals to 0.
// I use a for loop that has a let variable with two parameters price / array. The second bracket, I have sum += price. Then I return
// Number that will be the sum divided by the array and use the .toFixed method that specifies I have two decimals.
/*
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 @@ -47,9 +71,20 @@ 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
let changedPrices = [];
for (let arrayWithPrices of closingPricesForAllStocks) {
let changedPrice = arrayWithPrices[arrayWithPrices.length - 1] - arrayWithPrices[0];
changedPrices.push(Number(changedPrice.toFixed(2)));
}
return changedPrices;
}
// I start with a function getPriceChanges and have an array as my parameter. Inside the brickets I have a let variable that equals
// an empty array. I use for loop and start with a let variable arrayWithPrices of ... . Then i do a bracket inside the first bracket
// which means the let variable only execute 'til its closing bracket. It checks changedPrice equals arrayWithPrices that has its own
// array were I use the length method minus 1 minus arrayWithPrices start from 0. The changedPrice is pushed and price has two decimals
// which is achieved by using the .toFixed method.

/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Expand All @@ -65,7 +100,21 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}
let arrayWithStrings = [];
for (let i = 0; i < stocks.length; i++) {
let stockOfName = stocks[i].toUpperCase();
let sortedArray = closingPricesForAllStocks[i].sort(function (a, b) {

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 looks good - I think it works.
There are a few other ways to do this as well - have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

return a-b});
let highestPrice = sortedArray[sortedArray.length -1];
let convertedPrice = highestPrice.toFixed(2);
arrayWithStrings.push(`The highest price of ${stockOfName} in the last 5 days was ${convertedPrice}`);

}
return arrayWithStrings;
// http://www.collectionsjs.com/sorted-array

}



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