Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London_Class_10 -Mehmet_Omer_Demir - JS-Core-1- Week-3 #282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
// Example 1
let a;
console.log(a);

//a dosen't have value

// Example 2
function sayHello() {
let message = "Hello";
}
// This varibale is defined only inside of function and cannot be accessed outside the function

let hello = sayHello();
console.log(hello);
Expand All @@ -29,8 +30,9 @@ function sayHelloToUser(user) {
}

sayHelloToUser();

// since the "sayHello()" function does not return any value, the variable "hello" is set to undefined

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// arr dosent have index 3
11 changes: 10 additions & 1 deletion 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
*/

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

for (let i=1; i<= 10 ;i++){
numbers.push(i)
}


// 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]; // complete this statement
}

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

/*
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)
numbers[0]=1

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 4 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const AGES = [
49
];


// 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: 8 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,14 @@ const BIRTHDAYS = [
];

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

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

function getTemperatureReport(cities) {
// TODO
let report = [];
for(let city of cities) {
let temperature = temperatureService(city);
report.push(`The temperature in ${city} is ${temperature} degrees`);
}
return report;
}


Expand Down
61 changes: 49 additions & 12 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
let headlines=[];
for (let title of allArticleTitles )
if(title.length <= 65){
headlines.push(title);
}
return headlines;
// TODO
}
}function titleWithFewestWords(allArticleTitles) {
let fewestWordsSoFar;
let titleWithFewestWords;

/*
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) {
// TODO
for(let title of allArticleTitles) {

// working out the number of words in the title by splitting on the space character
// this will generate an array. Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
let numWords = title.split(' ').length;

if(fewestWordsSoFar === undefined || numWords < fewestWordsSoFar) {
fewestWordsSoFar = numWords;
titleWithFewestWords = title;
}
}

return titleWithFewestWords;
}

/*
Expand All @@ -23,19 +37,42 @@ 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 articlesWithNumbers = [];

for(let title of allArticleTitles) {
// Making use of the new function created below
if(doesTitleContainANumber(title)) {
articlesWithNumbers.push(title);
}
}

return articlesWithNumbers;
}

// Creating another function to help break this problem down into smaller parts
function doesTitleContainANumber(title) {
for(let character of title) {
if(character >= '0' && character <= '9') {
return true;
}
}

return false;
}

/*
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 totalCharacters = 0;

for(let title of allArticleTitles) {
totalCharacters += title.length;
}

return Math.round(totalCharacters / allArticleTitles.length);
}
/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
107 changes: 101 additions & 6 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :)
THESE EXERCISES ARE QUITE HARD. DON'T WORRY IF YOU CAN'T COMPLETE THEM ALL - JUST DO YOUR BEST, AND COME WITH QUESTIONS :)

Imagine we a working for a finance company. Below we have:
- an array of stock tickers
Expand Down Expand Up @@ -33,8 +33,41 @@ 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!
*/

/*
SOLUTION EXPLANATION: This is a complex problem and there are many ways to solve it!
I've decided to break the large problem down into a few smaller problems.
1. The exercise is asking us to find the average price of EACH stock.
So first, maybe we can just work out how to get the average price for a SINGLE stock.
For this, I've created a separate function called getAveragePricesForStock.
2. We also need to work out how to round a number to 2 decimal places.
There's also a separate function for this called roundTo2Decimals.
3. We can put these smaller solutions back together to solve the larger problem.
The top-level getAveragePrices prices function can loop through the array,
and pass each sub-array to the getAveragePricesForStock function.
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averages = [];

for(let pricesForStock of closingPricesForAllStocks) {
averages.push(getAveragePricesForStock(pricesForStock));
}

return averages;
}

function getAveragePricesForStock(pricesForStock) {
let total = 0;

for(let price of pricesForStock) {
total += price;
}

return roundTo2Decimals(total / pricesForStock.length);
}

function roundTo2Decimals(num) {
return Math.round(num * 100) / 100;
}

/*
Expand All @@ -47,8 +80,26 @@ 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)
*/

/*
SOLUTION EXPLANATION: Again, we can break the large problem down into a few smaller problems.
1. I've created a new function which just calculates the change in price for a SINGLE stock - getPriceChangeForStock.
2. This new function can also use the roundTo2Decimals function we implemented earlier.
One of the many advatages of using functions is being able to re-use code!
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let changes = [];

for(let pricesForStock of closingPricesForAllStocks) {
changes.push(getPriceChangeForStock(pricesForStock))
}

return changes;
}

function getPriceChangeForStock(pricesForStock) {
let priceChange = pricesForStock[pricesForStock.length - 1] - pricesForStock[0]
return roundTo2Decimals(priceChange);
}

/*
Expand All @@ -61,10 +112,54 @@ function getPriceChanges(closingPricesForAllStocks) {
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
The price should be shown with EXACTLY 2 decimal places.
*/

/*
SOLUTION EXPLANATION: We can also break this problem down into a smaller problems.
1. I've created a new function which just calculates the highest price for a SINGLE stock - getHighestPrice.
2. This new function can also use the roundTo2Decimals function we implemented earlier.
ALTERNATE SOLUTION: See highestPriceDescriptionsAlternate
I've included an alternate solution here which makes use of JavaScript's Math.max() function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
The Math.max() function takes zero or more numbers as input parameters.
We can convert our array into individual numbers that can then be passed into this function using the spread syntax: ...
Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let descriptions = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = getHighestPrice(closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}

return descriptions;
}

function getHighestPrice(pricesForStock) {
// initialising to 0, as we're expecting this value to be overriden by the first price in the array
let highestPriceSoFar = 0;

for(let price of pricesForStock) {
// if this price is higher than the highest price we've seen so far, it becomes the new highest price
if(price > highestPriceSoFar) {
highestPriceSoFar = price;
}
}

return highestPriceSoFar;
}

function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) {
let descriptions = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = Math.max(...closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}

return descriptions;
}


Expand All @@ -91,4 +186,4 @@ test("should return a description of the highest price for each stock", () => {
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
});
});