From 04a902077aa9696e3073131f76a373ec75b74cb1 Mon Sep 17 00:00:00 2001 From: Nishka-Kisten <103143568+Nishka-Kisten@users.noreply.github.com> Date: Fri, 15 Jul 2022 23:43:53 +0200 Subject: [PATCH 1/2] Nishka_Kisten --- 1-exercises/A-undefined/exercise.js | 5 +- 1-exercises/B-while-loop/exercise.js | 16 +++++-- .../C-while-loop-with-array/exercise.js | 7 ++- 1-exercises/D-do-while/exercise.js | 8 +++- 1-exercises/E-for-loop/exercise1.js | 6 +-- 1-exercises/E-for-loop/exercise2.js | 3 ++ 1-exercises/F-for-of-loop/exercise.js | 12 ++++- 2-mandatory/1-weather-report.js | 12 +++-- 2-mandatory/2-retrying-random-numbers.js | 6 +++ 2-mandatory/3-financial-times.js | 46 +++++++++++++++---- 2-mandatory/4-stocks.js | 40 ++++++++++++++-- 11 files changed, 133 insertions(+), 28 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..1a97cf4e 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,11 +12,12 @@ // Example 1 let a; console.log(a); - +// The variable a has not been declared. // Example 2 function sayHello() { let message = "Hello"; +// no 'return message' in this function. } let hello = sayHello(); @@ -26,6 +27,7 @@ console.log(hello); // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); + // user has no value/ not defined. } sayHelloToUser(); @@ -34,3 +36,4 @@ sayHelloToUser(); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// arr[3] calls for the 4th number in the array, there are only 3 numbers in this array. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8036c36d 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -5,10 +5,16 @@ The list of numbers should start with 0. n is being passed in as a parameter. */ -function evenNumbers(n) { - // TODO +function evenNumbers(n) { + let i = 0; + let array =[]; + while (i < n ) { + array.push(i * 2); + i++; + } + return array.join(); } -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 +console.log(evenNumbers(0)); // should output nothing +console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..c8d80534 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,12 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < birthdays.length) { + if (birthdays[i].includes("July")){ + return birthdays[i];} + + i++;} } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..3bcf17f2 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,13 @@ */ function evenNumbersSum(n) { - // TODO + let i = 0; + let array =[]; + do{ + array.push(i * 2 ); + i++; + } while(i < n); + return array.reduce((a,b) => a + b, 0); } console.log(evenNumbersSum(3)); // should output 6 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..efa749da 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -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++ ){ console.log(String.fromCharCode(97 + i)); - i++; } // The output shouldn't change. + + diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..ae4ced98 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here +for(let i = 0; i < 5; i++ ){ + console.log(WRITERS[i] + " is " + AGES[i] + " years old ") +} /* The output should look something like this: diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..a2ab1114 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -1,5 +1,5 @@ /* - A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). + A for-of loop is an easy way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). */ // TODO Use a for-of loop to output each of the tube stations below. @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let value of tubeStations) { +console.log(value); +} + + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. -let str = "codeyourfuture"; +let str = "codeyourfuture"; +for(let val of str) { + console.log(val.toUpperCase()); +} \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..d2a6e120 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -2,7 +2,7 @@ Imagine we're making a weather app! We have a list of cities that the user wants to track. - We also already have a temperatureService function which will take a city as a parameter and return a temparature. + We also already have a temperatureService function which will take a city as a parameter and return a temperature. Implement the function below: - take the array of cities as a parameter @@ -10,9 +10,13 @@ For example, "The temperature in London is 10 degrees" - Hint: you can call the temperatureService function from your function */ - -function getTemperatureReport(cities) { - // TODO + function getTemperatureReport(usersCities ){ + let result = []; + for(let i = 0; i < usersCities.length; i++ ){ + let message= "The temperature in " + usersCities[i] + " is " + temperatureService(usersCities[i]) + " degrees"; + // let message = "The temperature in " + usersCity[i] + " is " + temperatureService[i] + " degrees"; + result.push(message); + } return result; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..f01135f2 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,12 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let i ; + do{ + i = generateRandomNumber(); + + }while(i <= 50) + return i ; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..098ada37 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,33 +5,63 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + var result = allArticleTitles.filter((n) => n.length <= 65) + if (result) { + return result; + } } + + /* 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) -*/ + (you can assume words will always be separated by a space) +*/ function titleWithFewestWords(allArticleTitles) { - // TODO -} + // return allArticleTitles.reduce( function(shortest, e) { + // return (typeof e == 'string') && (shortest=='' || e.length < shortest.length) ? e : shortest; + // }, ''); + var fewestWords = allArticleTitles[0]; + for (let i = 0; i < allArticleTitles.length; i++) { + var element = allArticleTitles[i]; + if (fewestWords.length > element.length) { + fewestWords = element; + } + } + return fewestWords; + }; + /* - The editor of the FT has realised that headlines which have numbers in them get more clicks! + The editor of the FT has realized 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 + // if(typeof allArticleTitles === "number") { + // return typeof allArticleTitles === "number"; + // } + titleWithNum = []; + var hasNumber = /\d/; + for(let element of allArticleTitles) { + if (hasNumber.test(element)){ + titleWithNum.push(element);} + } return titleWithNum; } + /* 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 total = 0; + let sum; + for(let element of allArticleTitles){ + total= total + element.length; + sum = Math.round(total/allArticleTitles.length); + }return sum; } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..a52cf3e1 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let total= []; + let tot= 0; + let sum = 0; + for(let element of closingPricesForAllStocks){ + sum = element.reduce((a, b) => a + b); + tot = sum / 5; + total.push(Math.round(tot * 100) / 100); + } return total } /* @@ -48,7 +55,18 @@ 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 total = []; + let priceChange = 0; + for (let prices of closingPricesForAllStocks) { + let last = prices.slice(-1); + // console.log(last); + // console.log(prices[0]); + priceChange = last - prices[0]; + // total.push(priceChange.toFixed(2)); + total.push( Math.round(priceChange * 100) / 100); + + // console.log(priceChange); + } return total; } /* @@ -64,7 +82,23 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let newStocks = []; +let total = []; +let message = ""; +let last; +for (let stock of closingPricesForAllStocks) { + stock.sort((a, b) => b - a); +// last = stock[0]; +// total.push( Math.round(last * 100) / 100); + total.push(stock); +} +for (let i = 0; i < stocks.length; i++) { + let roundedUp =(total[i][0]).toFixed(2); + // let roundedUp = Math.round((total[i][0]) * 100) / 100; + message = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + roundedUp; + newStocks.push(message); +} +return newStocks; } From 2fa40680b458f5c15d57ebbf4279efa885f7f8aa Mon Sep 17 00:00:00 2001 From: Nishka-Kisten <103143568+Nishka-Kisten@users.noreply.github.com> Date: Fri, 15 Jul 2022 23:45:58 +0200 Subject: [PATCH 2/2] Update 3-financial-times.js --- 2-mandatory/3-financial-times.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 098ada37..af062267 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -19,9 +19,6 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { - // return allArticleTitles.reduce( function(shortest, e) { - // return (typeof e == 'string') && (shortest=='' || e.length < shortest.length) ? e : shortest; - // }, ''); var fewestWords = allArticleTitles[0]; for (let i = 0; i < allArticleTitles.length; i++) { var element = allArticleTitles[i];