-
-
Notifications
You must be signed in to change notification settings - Fork 279
NW5- LEEDS - CUNEYT TURKER - JAVASCRIPT1 WEEK 3 #128
base: main
Are you sure you want to change the base?
Changes from all commits
b9afc58
fa7f8d7
8dcce73
31d72e1
3d570ca
2498434
7236f71
ab09475
ebb266a
37b60b9
c8653a4
1b95243
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 |
|---|---|---|
|
|
@@ -10,27 +10,27 @@ | |
| */ | ||
|
|
||
| // Example 1 | ||
| let a; | ||
| let a; // There is nothing equal with variable "a" | ||
| console.log(a); | ||
|
|
||
|
|
||
| // Example 2 | ||
| function sayHello() { | ||
| let message = "Hello"; | ||
| let message = "Hello";// This function does not return anything, so definition is not enough. | ||
| } | ||
|
|
||
| let hello = sayHello(); | ||
| console.log(hello); | ||
| console.log(hello);// Variable defined but function must be defined too. | ||
|
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. Well spotted, there are no parethesis |
||
|
|
||
|
|
||
| // Example 3 | ||
| function sayHelloToUser(user) { | ||
| console.log(`Hello ${user}`); | ||
| } | ||
|
|
||
| sayHelloToUser(); | ||
| sayHelloToUser(); // There must be a value inside of the paranthesis to make our function defined. | ||
|
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 function will be defined without a value, we're just calling it wrong, so you are correct we need a value. |
||
|
|
||
|
|
||
| // Example 4 | ||
| let arr = [1,2,3]; | ||
| console.log(arr[3]); | ||
| console.log(arr[3]); // The index has been given is not existing or NOT DEFINED in the variable | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,13 @@ | |
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| let output = []; | ||
| let i =0; | ||
| while(output.length<n){ | ||
|
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'm afraid this doesn't work Cuneyt, if I call this function with
Let me know if you want help with this one in the group chat. |
||
| output.push(i); | ||
| i=i+2 | ||
| } | ||
| console.log(output) | ||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,11 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let output = [] | ||
| for(let i of cities){ | ||
| output.push(`The temperature in ${i} is ${temperatureService(i)} 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. Great use of backticks here |
||
| } | ||
| return output | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,18 @@ | |
|
|
||
| // This function shouldn't be changed | ||
| function generateRandomNumber() { | ||
| console.log("Generating number..."); | ||
| console.log("Generating number...") | ||
| return Math.round(Math.random() * 100); | ||
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| } | ||
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| let numbersInArray = [] | ||
| do{ | ||
| numbersInArray = generateRandomNumber() | ||
| }while(numbersInArray<51) | ||
|
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'm not sure why you used an array here, it does seem to work but maybe you can teach me something after this review 😂 I'd have gone for an integer / Number. |
||
| return numbersInArray | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
||
| 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 fitTitles = []; | ||
| for(let i in allArticleTitles){ | ||
| if(allArticleTitles[i].length<=65){ | ||
| fitTitles.push(allArticleTitles[i]) | ||
| } | ||
| } | ||
| return fitTitles | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -14,24 +20,48 @@ function potentialHeadlines(allArticleTitles) { | |
| (you can assume words will always be seperated by a space) | ||
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| let lengthOfTitles = [] | ||
| for(let i in allArticleTitles){ | ||
| lengthOfTitles.push(allArticleTitles[i].split(" ").length) | ||
| } | ||
| let minimumTitlesIndex = lengthOfTitles.indexOf(Math.min(...lengthOfTitles)) | ||
|
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 is a little hard to read but you did it right. You created an array of words and then got the minimum and returned it. |
||
| return allArticleTitles[minimumTitlesIndex] | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /* | ||
| The editor of the FT has realised that headlines which have numbers in them get more clicks! | ||
| Implement the function below to return a new array containing all the headlines which contain a number. | ||
| (Hint: remember that you can also loop through the characters of a string if you need to) | ||
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| } | ||
| let output = [] | ||
| for(let i in allArticleTitles){ | ||
|
|
||
| if(allArticleTitles[i].match(/[0-9]/g) !== null){ | ||
|
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 use of regular expressions, @Ekremteke actually simplified this with the
Author
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. That made sense, thank you. |
||
| output.push(allArticleTitles[i]) | ||
| } | ||
|
|
||
| } | ||
| return output | ||
| } | ||
|
|
||
| /* | ||
| 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 allIntegers = [] | ||
| for(let i in allArticleTitles){ | ||
| allIntegers.push(allArticleTitles[i].length) | ||
| } | ||
| let total = 0; | ||
| for(let i = 0; i < allIntegers.length; i++) { | ||
| total += allIntegers[i]; | ||
| } | ||
| let average = total / allIntegers.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. You did great here, I would advise using new lines to help separate your code to help the readability. Split it up into chunks of separate logic. |
||
| return Math.round(average) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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 arrayOfAverage = [] | ||
| for(let i of closingPricesForAllStocks){ | ||
| let total = 0; | ||
| for(let j in i){ | ||
| total += i[j]; | ||
| } | ||
| let a = total/i.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. I'm not a huge advocate for your variable naming here, you have Other than that, good job! There are a couple other ways to do this but you succeeded in completed the exercise.
Author
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 really need to get used to it. |
||
| let b = Number(a.toFixed(2)) | ||
| arrayOfAverage.push(b) | ||
| } | ||
| return arrayOfAverage | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -48,7 +59,12 @@ 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 priceChanges = [] | ||
| for(let array of closingPricesForAllStocks){ | ||
| let output = array[array.length-1]-array[0]; | ||
| priceChanges.push(Number(output.toFixed(2))) | ||
| } | ||
| return priceChanges | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -64,7 +80,14 @@ function getPriceChanges(closingPricesForAllStocks) { | |
| The price should be shown with exactly 2 decimal places. | ||
| */ | ||
| function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
| // TODO | ||
| let returnThisArray = [] | ||
| for(let index in closingPricesForAllStocks){ | ||
| let largestNumber = Math.max(...closingPricesForAllStocks[index]); | ||
| let outputWithQuote = `The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${largestNumber.toFixed(2)}` | ||
| returnThisArray.push(outputWithQuote) | ||
| } | ||
| return returnThisArray | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,11 @@ | |
| */ | ||
|
|
||
| function factorial(input) { | ||
| // TODO | ||
| let total = 1; | ||
| for(let index=input;index>0;index--){ | ||
|
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. Always remember to include spaces, they're not needed by the compiler or processor which reads the code, but it helps us read it better as developers 😅 I'm not sure you actually completed this task, we can talk about it in the hawk channel if you want. |
||
| total *= index | ||
| } | ||
| return total | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,11 @@ | |
| */ | ||
|
|
||
| function getHighestRatedInEachGenre(books) { | ||
| // TODO | ||
| for(let everyObjects of books){ | ||
|
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 know this function isn't complete 😂 If you need help with this one, please talk in the hawk channel, myself and the others can help you out. |
||
| for(let key of everyObjects){ | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
You're correct, I think maybe a language issue, the best way to say it is that there is no value assigned to variable "a". Just because equal sounds like "a === a" etc but I knew what you meant 👍