-
-
Notifications
You must be signed in to change notification settings - Fork 279
JavaScript-core-1-Coursework-week3 Ebrahim Beiati-Asl #188
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,11 @@ const BIRTHDAYS = [ | |
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| let i = 0; | ||
| while (i < birthdays.length) { | ||
| if (birthdays[i].includes("July")) return birthdays[i]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is all good, but try to make sure to include |
||
| i++; | ||
| } | ||
| } | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output “July 11th” | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,20 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| } | ||
| let counter = 1; | ||
| let sum = 0; | ||
|
|
||
| do { | ||
| if (counter % 2 === 0) { | ||
| sum = sum + counter; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be cleaned up by using the |
||
| } | ||
| counter ++; | ||
| } while (counter <= n){ | ||
|
|
||
| return sum; | ||
| }} | ||
|
|
||
| // TODO | ||
| console.log(evenNumbersSum(3)); // should output 6 | ||
| console.log(evenNumbersSum(0)); // should output 0 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,52 +12,51 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let result = []; | ||
| for (let i = 0; i < cities.length; i++) { | ||
| result.push( | ||
| `The temperature in ${cities[i]} is ${temperatureService( | ||
| cities[i] | ||
| )} degrees` | ||
| ); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
| let temparatureMap = new Map(); | ||
|
|
||
| temparatureMap.set('London', 10); | ||
| temparatureMap.set('Paris', 12); | ||
| temparatureMap.set('Barcelona', 17); | ||
| temparatureMap.set('Dubai', 27); | ||
| temparatureMap.set('Mumbai', 29); | ||
| temparatureMap.set('São Paulo', 23); | ||
| temparatureMap.set('Lagos', 33); | ||
| return temparatureMap.get(city); | ||
| let temparatureMap = new Map(); | ||
|
|
||
| temparatureMap.set("London", 10); | ||
| temparatureMap.set("Paris", 12); | ||
| temparatureMap.set("Barcelona", 17); | ||
| temparatureMap.set("Dubai", 27); | ||
| temparatureMap.set("Mumbai", 29); | ||
| temparatureMap.set("São Paulo", 23); | ||
| temparatureMap.set("Lagos", 33); | ||
|
|
||
| return temparatureMap.get(city); | ||
|
Comment on lines
-22
to
+38
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think your formatter settings conflict with the repo here ; is not a huge deal but something to watch out for |
||
| } | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
| let usersCities = [ | ||
| "London", | ||
| "Paris", | ||
| "São Paulo" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees" | ||
| ]); | ||
| let usersCities = ["London", "Paris", "São Paulo"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return a temperature report for the user's cities (alternate input)", () => { | ||
| let usersCities = [ | ||
| "Barcelona", | ||
| "Dubai" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees" | ||
| ]); | ||
| let usersCities = ["Barcelona", "Dubai"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return an empty array if the user hasn't selected any cities", () => { | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
|
|
||
| return allArticleTitles.filter(items=> items.length < 65); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of an arrow function! although to be a bit cleaner the |
||
| // TODO | ||
| } | ||
|
|
||
|
|
@@ -14,6 +16,11 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| let title = []; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| title.push(allArticleTitles [i].split (' ').length); | ||
| } | ||
| return allArticleTitles[title.indexOf(Math.min(...title))]; | ||
| // TODO | ||
| } | ||
|
|
||
|
|
@@ -24,13 +31,22 @@ function titleWithFewestWords(allArticleTitles) { | |
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| return allArticleTitles.filter((element) => | ||
| [...element].find((number) => number >= "0" && number <= "9") | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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) { | ||
| let sum = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| sum += allArticleTitles[i].length; | ||
| } | ||
| return Math.round(sum / allArticleTitles.length); | ||
| // TODO | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,10 +33,28 @@ 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! | ||
| */ | ||
| function calculateAverage(newPrice) { | ||
| let sum = 0; | ||
| for (let price of newPrice) { | ||
| sum += price; | ||
| } | ||
| const average = sum / newPrice.length; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if |
||
| return average; | ||
| } | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| // TODO | ||
| const myPrice = []; | ||
| for (let stock of closingPricesForAllStocks) { | ||
| const average = calculateAverage(stock); | ||
| const formattedAverage = Number(average.toFixed(2)); | ||
| myPrice.push(formattedAverage); | ||
| } | ||
| return myPrice; | ||
| } | ||
|
|
||
| // let stocks = ["aapl", "msft", "amzn", "googl", "tsla"]; | ||
| // showStocks(stocks); | ||
| // changeInPrices(closingPricesLast5Days); | ||
| /* | ||
| 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 | ||
|
|
@@ -48,6 +66,14 @@ 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) { | ||
| let myPrice1 = []; | ||
| for (let price of closingPricesForAllStocks) { | ||
| let firstDay = price[0]; | ||
| let lastDay = price[price.length - 1]; | ||
| let difference = lastDay - firstDay; | ||
| myPrice1.push(Number(difference.toFixed(2))); | ||
| } | ||
| return myPrice1; | ||
| // TODO | ||
| } | ||
|
|
||
|
|
@@ -64,6 +90,17 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| let newPrice = []; | ||
| for (let i = 0; i < closingPricesForAllStocks.length; i++) { | ||
| newPrice.push( | ||
| `The highest price of ${stocks[ | ||
| i | ||
| ].toUpperCase()} in the last 5 days was ${Math.max( | ||
| ...closingPricesForAllStocks[i] | ||
| ).toFixed(2)}` | ||
| ); | ||
| } | ||
| return newPrice; | ||
| // TODO | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,11 @@ | |
| */ | ||
|
|
||
| function factorial(input) { | ||
| let sum = 1; | ||
| for (i = 1; i <= input; i++) { | ||
| sum *= i; | ||
| } | ||
| return sum; | ||
| // TODO | ||
|
Comment on lines
11
to
17
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good to see you doing the extra ones! :) |
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right idea, but you've created an infinite loop here (
while i<=nbut you only incrementiifi%2===0, so when it reachesi=1you infinitely loop.What you need is two variables; one to increment on every iteration up to
n, and one hold the even numbers