-
-
Notifications
You must be signed in to change notification settings - Fork 279
WM4 - Robert Csonka - JS1week3 #101
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 |
|---|---|---|
|
|
@@ -17,7 +17,16 @@ const BIRTHDAYS = [ | |
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| let i = 0; | ||
| let birthday; | ||
| while (i < birthdays.length) { | ||
| if (birthdays[i].includes("July")) { | ||
| birthday = birthdays[i]; | ||
| i++ | ||
| } | ||
| i++; | ||
| } | ||
| return birthday | ||
|
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. A well structured implementation of the function. However, the function will continue to search the presence of July up till the end even after it found the first July. The presence of |
||
| } | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,10 @@ const AGES = [ | |
| 49 | ||
| ]; | ||
|
|
||
| for (let i = 0; i < 5; i++) { | ||
| 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. Excellent! |
||
| // TODO - Write for loop code here | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,13 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let arr = []; | ||
| for (const element of cities) { | ||
| if (temperatureService(element)) { | ||
| arr.push(`The temperature in ${element} is ${temperatureService(element)} degrees`); | ||
|
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 approach! |
||
| } | ||
| } return arr; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| function temperatureService(city) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,13 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let arr = []; | ||
| for (const element of allArticleTitles) { | ||
| if (element.length <= 65) { | ||
| arr.push(element); | ||
| } | ||
| }return arr; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| let arrLength = []; | ||
| let smallestNumValue; | ||
| let index; | ||
| for (const element of allArticleTitles) { //my first step was to itirate through the array and create an other array with indexes of lenght of each string | ||
| arrLength.push(element.split(" ").length); | ||
| } | ||
| smallestNumValue = Math.min(...arrLength); //once i had the array of indexes of lenght I went on to find the position of the smallest index in the array in 2 steps, first finding the value of the smallest number | ||
| index = arrLength.indexOf(smallestNumValue)//this is the second step of finding the position of the smallest index in an array, this steps finds the position of the smallest number that we identified in the previous step | ||
| return allArticleTitles[index]; //finally i return the position of the smallest number of the original array | ||
|
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 approach is a smart one! |
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -23,19 +37,35 @@ 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 array = [] | ||
| for (const element of allArticleTitles){ | ||
| if ( /\d/.test(element)) { | ||
| array.push(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. A very good use of regular expression! |
||
| } return array; | ||
| } | ||
|
|
||
| /* | ||
| 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 arrNumOfCharacters = []; | ||
| let total = 0; | ||
| let average = 0; | ||
| for (const element of allArticleTitles) { | ||
| arrNumOfCharacters.push(element.trim().length); | ||
| } | ||
| for (const element of arrNumOfCharacters) { | ||
| total = total + 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. The way you declare variables at the beginning is fantastic. |
||
| average = total / arrNumOfCharacters.length; | ||
| return Math.round(average); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= List of Articles - DO NOT MODIFY ===== */ | ||
| const ARTICLE_TITLES = [ | ||
| "Streaming wars drive media groups to spend more than $100bn on new content", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
| Functions can help with this! | ||
| */ | ||
| function getAveragePrices(closingPricesForAllStocks) { | ||
| // TODO | ||
| let total = 0; | ||
| let average = 0; | ||
| arrayOfAverages = []; | ||
| for (const subArray of closingPricesForAllStocks) { | ||
| for (const element of subArray) { | ||
| total = total + element; | ||
| } | ||
| average = total / subArray.length; | ||
| total = 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. I like the creative way of resetting the value of |
||
| arrayOfAverages.push(Math.round(average *100)/100); | ||
| } | ||
| return arrayOfAverages; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +59,15 @@ 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 priceDifference = 0; | ||
| let arrayOfChanges = []; | ||
| for (const subArray of closingPricesForAllStocks) { | ||
|
|
||
| priceDifference = Math.round((subArray[subArray.length -1] - subArray[0]) * 100) /100; | ||
| arrayOfChanges.push(priceDifference); | ||
| } | ||
|
|
||
| return arrayOfChanges; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +83,17 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let highestPriceEach = 0; | ||
| let arrayOfPrices = []; | ||
| let i = 0; | ||
| for (const subArray of closingPricesForAllStocks) { | ||
|
|
||
| highestPriceEach = Math.max(...subArray).toFixed(2); | ||
| arrayOfPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPriceEach}`); | ||
| i++ | ||
|
|
||
| } | ||
| return arrayOfPrices; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,29 @@ | |
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| const children = []; | ||
| const nonFiction = []; | ||
| const cooking = []; | ||
| const highestRatedTitles = []; | ||
| for (let i = 0; i < books.length; i++) { | ||
| if (books[i].genre === "children") { | ||
| children.push(books[i]); | ||
| } else if (books[i].genre === "non-fiction") { | ||
| nonFiction.push(books[i]); | ||
| } else { | ||
| cooking.push(books[i]); | ||
| } | ||
| } | ||
| children.sort((a, b) => b.rating - a.rating); | ||
|
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. A beautiful way of using arrow function! |
||
| nonFiction.sort((a, b) => b.rating - a.rating); | ||
| cooking.sort((a, b) => b.rating - a.rating); | ||
| highestRatedTitles.push(children[0].title, nonFiction[0].title, cooking[0].title) | ||
| return highestRatedTitles; | ||
| } | ||
|
|
||
| // let numbers = [2, 5, 2, 5, 6, 8, 86,34, 0, 22]; | ||
| // console.log(numbers.sort((a, b) => b - a)) | ||
|
|
||
|
|
||
| /* ======= Book data - DO NOT MODIFY ===== */ | ||
| const BOOKS = [ | ||
|
|
@@ -69,6 +90,7 @@ const BOOKS = [ | |
| }, | ||
| ] | ||
|
|
||
| // console.log(getHighestRatedInEachGenre(BOOKS)) | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| test("should return the highest rated book in each genre", () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,13 @@ | |
| */ | ||
|
|
||
| function generateFibonacciSequence(n) { | ||
| // TODO | ||
| arr = [0]; | ||
| num = 1; | ||
| for (let i = 0; i < n - 1; i++) { | ||
| arr.push(num); | ||
| num = num + arr[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. Nice solution! |
||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| function logicalCalc(array, op){ | ||
| //your code here | ||
| let logic | ||
| // for (let i = 0; i < array.length; i++) { | ||
| if (op === "AND") { | ||
| logic = array.reduce((a, b) => a && b); | ||
| } else if (op === "OR") { | ||
| logic = array.reduce((a, b) => a || b); | ||
| } else if ( op === "XOR") { | ||
| logic = array.reduce((a, b) => a != b); | ||
| } | ||
| // } | ||
| return logic | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function evenNumbers(n) { | ||
| let i = 0; | ||
| let arr = []; | ||
| while (i < n * 2) { | ||
| if (i % 2 === 0) { | ||
| arr.push(i); | ||
| } | ||
| i++; | ||
| } | ||
| console.log(arr); | ||
| } | ||
|
|
||
| evenNumbers(10); |
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.
This is an excellent explanation and good use of comments.