Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
11 changes: 6 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

// Example 1
let a;
console.log(a);
console.log(a); //we didn't assign a value to the variable a


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; // the function sayHello() doesn't return anything
}

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


Expand All @@ -28,9 +28,10 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); // we didn't insert any input in the function sayHelloToUser()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thought: The function will output the string "Hello undefined" instead of just undefined



// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // the array arr is containing 3 elements with idexes 0,1,2. The index 3 is not avaiable in the array arr

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thought: This will return undefined if the array is empty.

}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thought: This will return an error if the arr value is not an array

}

/*
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[3] = 4;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: You could also do

numbers.push(4);

numbers[0] = 1;

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
3 changes: 3 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ 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
10 changes: 9 additions & 1 deletion 1-exercises/E-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
// for (let i = 0; i < birthdays.length; i++) {
// if (birthdays[i].charAt("July")) {
// return birthdays[i]
// }
// }

const firstBirthdayInJuly = birthdays.find (element => element == "July")
return firstBirthdayInJuly

}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
12 changes: 12 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@

function getTemperatureReport(cities) {
// TODO
let resultArray = []
for (let city of cities) {
let text = "The temperature in "+ city + " is " + temperatureService(city) + " degrees";
resultArray.push(text)

}
return resultArray
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: I saw you and Elena have a similar solution.

I had another solution
function getTemperatureReport(cities) {
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;
}





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

function temperatureService(city) {
Expand All @@ -31,6 +40,9 @@ function temperatureService(city) {

return temparatureMap.get(city);
}
// test("Should return an array of the same argument", () => {
// expect(getTemperatureReport(usersCities).length).toEqual(3)
// });

test("should return a temperature report for the user's cities", () => {
let usersCities = [
Expand Down
49 changes: 46 additions & 3 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let articleTitlesLessThan65 = []
for (article of allArticleTitles) {
if (article.length <= 65) {
articleTitlesLessThan65.push(article)
}
}
return articleTitlesLessThan65

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: This could also be achieved by using a .filter() function


}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Same as mine


/*
Expand All @@ -14,7 +22,27 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO

let shortestTitle;
// let shortestTitleWordCount = Infinity;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nitpick: Don't leave commented code in your solution

let shortestTitleWordCount;


let firstWord;
for (let title of allArticleTitles) {

let wordCount = title.split(" ").length;
if (wordCount < (shortestTitleWordCount) || (shortestTitle === undefined)){
shortestTitleWordCount = wordCount
shortestTitle = title
}


}
return shortestTitle

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: I see you use .split which I did not. Well done

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: This could also be achieved with a reduce() function.


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nitpick: Don't leave too many blank lines in a function



}

/*
Expand All @@ -23,15 +51,30 @@ 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 arrayTitlesWithNumbers =[]
for (let title of allArticleTitles) {
if (/\d/.test(title)) {
arrayTitlesWithNumbers.push(title)
}
}
return arrayTitlesWithNumbers
}


/*
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 sum = 0;
let count = 0;
for (let title of allArticleTitles) {
let wordCount = title.length;
sum += wordCount;
count += 1;
}
let average = sum / count;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

couldnt you use allArticleTitles.length - 1 instead count variable ?

return Math.round(average);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Nice

}


Expand Down
59 changes: 56 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let sum = 0;
let averageArray = []
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++){
sum += closingPricesForAllStocks[i][j]
}

let average = sum / 5;
averageArray.push(Math.round(average *100)/100)
sum = 0;
}
return averageArray

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Nice code!

}

/*
Expand All @@ -48,7 +59,19 @@ 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
let priceChangeArray = []
let priceChange = 0
let priceOnFirstDay = 0
let priceOnLastDay = 0
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
priceOnFirstDay = closingPricesForAllStocks[i][0]
priceOnLastDay = closingPricesForAllStocks[i][4]
}
priceChange = priceOnLastDay - priceOnFirstDay
priceChangeArray.push(Math.round(priceChange * 100) / 100)
}
return priceChangeArray
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: You could also use array in array method.
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;
}


/*
Expand All @@ -63,11 +86,41 @@ function getPriceChanges(closingPricesForAllStocks) {
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/

function highestPriceFunction (closingPricesForAllStocks) {
let highestPriceArray = []
let price = 0
let highestPrice = 0
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
if (closingPricesForAllStocks[i][j] > price) {
highestPrice = closingPricesForAllStocks[i][j]
price = highestPrice
}
}
highestPriceArray.push(highestPrice)
price = 0
}
return highestPriceArray

}

function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let resultArray = []
highestPriceArray = highestPriceFunction (closingPricesForAllStocks)
for (let i = 0; i < 5; i++) {
for(let j = 0; j < 5; j++) {
if (i === j) {
resultArray.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestPriceArray[j].toFixed(2))
}
}
}
return resultArray
}




/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand Down