-
-
Notifications
You must be signed in to change notification settings - Fork 279
Ezgi-Gunes-JavaScript-Core-1-Coursework-Week3-London8 #27
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 |
|---|---|---|
|
|
@@ -7,8 +7,17 @@ | |
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| let even=[] | ||
| let i =0 | ||
|
|
||
| while(even.length<n){ | ||
| even.push(i); | ||
| i+=2; | ||
|
|
||
| }return even; | ||
|
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. The final output is correct except it should return a string. Think of a method that allows you to do that. |
||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
| evenNumbers(0); // should output nothing | ||
| evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 | ||
|
|
||
| console.log(evenNumbers(3)); // should output 0,2,4 | ||
|
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. So, technically, according to the instructions, the function should |
||
| console.log(evenNumbers(0)); // should output nothing | ||
| console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,6 @@ const BIRTHDAYS = [ | |
| "November 15th" | ||
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| } | ||
| const findFirstJulyBDay = BIRTHDAYS.find(e=>e.includes('July')); | ||
|
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. The console displays an error message saying it is not a function. There might be some syntax problems here. 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. While this is a clever solution and kind of does solve the problem, you should try and implement things as asked by the exercise, rather than modify the check. So, here for example, you could do function findFirstJulyBDay(birthdays) {
return birthdays.find(e=>e.includes('July'));
}To create a function. Also, this should technically be a "while" loop exercise -- how would you complete it with a while loop? |
||
| console.log(findFirstJulyBDay); | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,16 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| } | ||
| let startNumber = 0; | ||
| let sum = 0; | ||
| do { | ||
| sum += startNumber * 2; | ||
|
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 :) |
||
| startNumber++; | ||
| } while (startNumber < n); | ||
| return sum; | ||
| } | ||
|
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. Your code would work if you placed your changes inside the function. 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. The code does work :) Maybe it looked confusing on a github diff? |
||
|
|
||
|
|
||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
| console.log(evenNumbersSum(0)); // should output 0 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,9 @@ | |
|
|
||
|
|
||
| // Change the below code to use a for loop instead of a while loop. | ||
| let i = 0; | ||
| while(i < 26) { | ||
|
|
||
| for(let i = 0; i < 26; i++) { | ||
|
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:) |
||
| console.log(String.fromCharCode(97 + i)); | ||
| i++; | ||
|
|
||
| } | ||
| // The output shouldn't change. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,14 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
| for (var i = 0; i < WRITERS.length; i++) { | ||
|
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. Try to use |
||
|
|
||
| console.log(`${WRITERS[i]} is ${AGES[i]} years old.`) | ||
|
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 use of template literals. |
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* | ||
| The output should look something like this: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,19 @@ let tubeStations = [ | |
| "Oxford Street", | ||
| "Tottenham Court Road" | ||
| ]; | ||
| for(let element of tubeStations){ | ||
|
|
||
| console.log(element); | ||
|
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 but get rid of the unnecessary spaces:) |
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| for(let element of str){ | ||
| const myArr=str.toUpperCase().split(''); | ||
|
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. I think you should target 'element' here not 'str'. The aim of this exercise is to output each letter separately, not the whole string. Also, I don't think you need split method here because it unnecessarily converts you string back to an array. |
||
| console.log(myArr); | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,17 @@ | |
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let cityWithTemperature = []; | ||
| for (let i = 0; i < cities.length; i++) { | ||
| let currentCity = cities[i]; | ||
| let currentCityTemperature = temperatureService(currentCity); | ||
|
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 usage of variable names! |
||
| cityWithTemperature.push( | ||
| `The temperature in ${currentCity} is ${currentCityTemperature} degrees` | ||
| ); | ||
| } | ||
| return cityWithTemperature; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| return allArticleTitles.filter((element) => element.length <= 65); | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -15,7 +17,31 @@ function potentialHeadlines(allArticleTitles) { | |
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| let shortestTitleLength = Infinity; | ||
| return allArticleTitles.reduce((acc, title) => { | ||
|
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. Indentation. |
||
| const titleLengthInWords = title.split(" ").length; | ||
| if (titleLengthInWords < shortestTitleLength) { | ||
| shortestTitleLength = titleLengthInWords; | ||
| return title; | ||
| } else { | ||
| return acc; | ||
| } | ||
| }, ""); | ||
| } | ||
| // function headlinesWithNumbers(allArticleTitles) { | ||
| //input is an array | ||
| //only return array entries that have numbers in | ||
| //return an array | ||
| // return allArticleTitles.filter((title) => { | ||
| // const matches = title.match(/\d/); | ||
| // if (matches !== null) { | ||
| // return true; | ||
| // } else { | ||
| // return false; | ||
| // } | ||
| // }); | ||
|
|
||
| // } | ||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
|
|
@@ -24,14 +50,31 @@ function titleWithFewestWords(allArticleTitles) { | |
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| const titlesToReturn = []; | ||
| const numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; | ||
| allArticleTitles.forEach((title) => { | ||
| title.split("").forEach((character) => { | ||
| if (numbers.includes(character)) { | ||
| titlesToReturn.push(title); | ||
| } | ||
| }); | ||
| }); | ||
| return titlesToReturn; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| 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 totalCharacterNumber = 0; | ||
| for (let i = 0; i < allArticleTitles.length; i++) { | ||
| totalCharacterNumber = | ||
| totalCharacterNumber + allArticleTitles[i].split("").length; | ||
|
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.
> str = "somestr"
'somestr'
> str.length
7 |
||
| } | ||
| return Math.round(totalCharacterNumber / allArticleTitles.length); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,22 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| } | ||
|
|
||
| return closingPricesForAllStocks.map((value, index) => { | ||
| let sum = 0, counter = 0; | ||
|
|
||
| for (let item of value) { | ||
| sum += item; | ||
| counter++; | ||
| } | ||
| average = parseFloat((sum/counter).toFixed(2)) | ||
|
|
||
| console.log(average); | ||
|
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. You should try and remove all |
||
| return average; | ||
| }) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| We also want to see what the change in price is from the first day to the last day for each stock. | ||
|
|
@@ -49,6 +64,14 @@ function getAveragePrices(closingPricesForAllStocks) { | |
| */ | ||
| function getPriceChanges(closingPricesForAllStocks) { | ||
| // TODO | ||
| let priceDiffArr=[]; | ||
|
|
||
| for(let price of closingPricesForAllStocks){ | ||
|
|
||
| let priceDiff=Number(price[price.length-1]-price[0]) | ||
|
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 |
||
| priceDiffArr.push(parseFloat(priceDiff.toFixed(2))) | ||
| } | ||
| return priceDiffArr; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -65,6 +88,14 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let highestPriceByCompany = []; | ||
| for( let i = 0; i < closingPricesForAllStocks.length; i++ ){ | ||
| let highestPrice = Math.max(...closingPricesForAllStocks[i]).toFixed(2); | ||
| highestPriceByCompany.push( | ||
| `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice}` | ||
| ); | ||
| } | ||
| return highestPriceByCompany; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,16 @@ | |
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| let bookTitles=[]; | ||
| for(let i=0; i<books.length; i++){ | ||
| if(books[i].rating > 4.8){ | ||
|
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 works for the exact test case provided, but wouldn't work with different inputs (for instance, if a genre's highest-rated book was rated 4.5), or there were two books rated > 4.8 in one genre. |
||
| bookTitles.push(books[i].title) | ||
| } | ||
|
|
||
| } | ||
| return bookTitles; | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,19 @@ | |
|
|
||
| function generateFibonacciSequence(n) { | ||
| // TODO | ||
|
|
||
| let fibonacci = [0, 1]; | ||
| for(i = 2; i < n; i++) { | ||
|
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. Great work on this one! |
||
| let y = fibonacci[i-2] + fibonacci[i-1]; | ||
| fibonacci.push(y); | ||
| } | ||
|
|
||
|
|
||
| return fibonacci; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the first 10 numbers in the Fibonacci Sequence", () => { | ||
| expect(generateFibonacciSequence(10)).toEqual( | ||
|
|
||
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.
👍