From ef749a510d4e8cc1d545efef51dbcdba381e512d Mon Sep 17 00:00:00 2001 From: George <108883931+GeorgePrimentas@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:51:45 +0000 Subject: [PATCH 1/2] Exercises & Mandatory exercises completed --- .DS_Store | Bin 0 -> 6148 bytes 1-exercises/A-undefined/exercise.js | 5 +- 1-exercises/B-while-loop/exercise.js | 70 ++++++++++++++++++ .../C-while-loop-with-array/exercise.js | 6 ++ 1-exercises/D-do-while/exercise.js | 8 ++ 1-exercises/E-for-loop/exercise1.js | 9 ++- 1-exercises/E-for-loop/exercise2.js | 5 ++ 1-exercises/F-for-of-loop/exercise.js | 8 ++ 2-mandatory/1-weather-report.js | 6 +- 2-mandatory/2-retrying-random-numbers.js | 12 +++ 2-mandatory/3-financial-times.js | 58 +++++++++++++++ 2-mandatory/4-stocks.js | 43 +++++++++++ 12 files changed, 225 insertions(+), 5 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a47903555cd784010173b49621721b3808077c4d GIT binary patch literal 6148 zcmeHK-EI;=82yH#EX5jPH0h1WCf))6zIvE(>_6#LSn>oca0A{G6Gc*&!m<9Jw{3A`uw~0<)WlmYAp)VZk!0 zWDCe-8+{s5k30&gzu>J4v;tazbyGlmy9KII8DH#sgx}w9c;xe*<8ucd;&?bfsQi$I z)Jm2!4hM?$)fxC+7*(s^SY{)8>2l7<83p5&^@fkF$c{!~(;mJMxen*P^DAvT&%FM) zTfBbAgUI%R{!kJ;w-1+>r(WRlag&b%HJbv2UrHHjZ_(>ZS?zOSsQX^d`|3)VyX>`o>G&JF^bMo zzR)MhzpD6MIfgEn2bjl$W!#KdiR7HG&;aYuqjSV(*v}Pui`$4(kkU%1oa{4?9@0{aXy#`0vifvG0LL@k$eIG zvnZB^wD?&N98+OK;Veefz*tNL##E-B7%Zlv+*E!Ig|irAIx+S5V5(=PUMNiVj`mII zPOK55jfq4Zs;%rge|LxW9|9O(mX$7j&L74EH o#V9F=)O9Qq;ws)okcK)_B!CTtvlvkV(?0@I1|4Yy{;C2$0q@x6g8%>k literal 0 HcmV?d00001 diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..b342f2c8 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,7 +12,7 @@ // Example 1 let a; console.log(a); - +// Variable a has not been initialised/assigned // Example 2 function sayHello() { @@ -21,6 +21,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +// The function sayHello() doesn't return any value // Example 3 @@ -29,8 +30,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); +// When the function is called (last line), no argument is given (for its user parameter) // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// Because of zero-indexing, arr[3] tries to find unsuccessfully the 4th element in the array arr (the array has only 3 elements) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..425dbd01 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -5,10 +5,80 @@ The list of numbers should start with 0. n is being passed in as a parameter. */ +// // Problematic solution (the numbers do not go in one line and there is a comma (,) after the last number) +// function evenNumbers(n) { +// // TODO +// let i = 0; +// while (i < n * 2) { +// if (i % 2 === 0) { +// console.log(i + ","); +// i++; +// } else { +// i++; +// } +// } +// } + + + +// function evenNumbers(n) { +// // TODO +// let i = 0; +// let string = "" +// while (i < n * 2) { +// if (i % 2 === 0) { +// string = string + i + ", "; +// i++; +// } else { +// i++; +// } + +// } +// string = string.slice(0,-2) +// // string = string.substring(0, string.length - 2) // Alternative but unnecessarily complicated +// console.log(string); +// } +// OK. This solution is correct but there must be a smarter way to do this than having to use slice... + +// // Solution with the use of an array +// function evenNumbers(n) { +// // TODO +// let i = 0; +// let array = [] +// while (i < n * 2) { +// if (i % 2 === 0) { +// array.push(i); +// i++; +// } else { +// i++; +// } +// } +// console.log(array); +// } + + + + +// Completely unnecessary use of %; overcomplicated problem-solving function evenNumbers(n) { // TODO + let i = 0; + let string = "" + while (i < n) { + string = string + (i * 2) + ", "; + i++; + } + string = string.slice(0,-2) + // string = string.substring(0, string.length - 2) // Alternative but unnecessarily complicated + console.log(string); } + + 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 + + + + diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..9bcce2ee 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,12 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + while (i < BIRTHDAYS.length) { + if (BIRTHDAYS[i].includes("July") === true) { + return BIRTHDAYS[i]; + } else 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..1b118df4 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,14 @@ function evenNumbersSum(n) { // TODO + let sum = 0 + let i = 0 + do { + sum += i; + i += 2; + } + while (i < n * 2); + return sum; } 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..7d274b6a 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,12 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +// let i = 0; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } +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..fade4988 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,8 +26,13 @@ const AGES = [ 49 ]; + // TODO - Write for loop code here +for (let i=0; i < WRITERS.length; 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..63be12bd 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let station of tubeStations) { + console.log(station) +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (string of str) { + console.log(string.toUpperCase()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..6bd4b0e7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,13 @@ function getTemperatureReport(cities) { // TODO + let cityStatement = [] + for (let city of cities) { + cityStatement.push("The temperature in " + city + " is " + temperatureService(city) + " degrees") + } + return cityStatement; } - /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..7fb51757 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,8 +11,20 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + do { + x = generateRandomNumber(); + } + while (x <= 50); +// console.log(x); +return x; } +// getRandomNumberGreaterThan50(); + +// Quite enlightening exercise to realise that looping is not only about having a variable periodically increasing/decreasing in order to achieve something +// It's more about a condition and its 'truthiness' (or 'falsiness') in which case a body of code has to be executed +// This applies specially to the while + /* ======= TESTS - DO NOT MODIFY ===== */ test("Returned value should always be greater than 50", () => { diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..eb98f3a4 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,13 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let articleTitlesUnder65 = [] + for (let title of allArticleTitles) { + if (title.length <= 65) { + articleTitlesUnder65.push(title) + } + } + return articleTitlesUnder65 } /* @@ -15,6 +22,37 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + // let word = 0 + // do { + // countingWords(allArticleTitles[word] + // ) + // } + + let articleTitle = 0; + let shortestTitle = 0 + let shortestTitleWords = countingWords(allArticleTitles[articleTitle]); + for (let articleTitle = 1; articleTitle < allArticleTitles.length; articleTitle++){ + if (countingWords(allArticleTitles[articleTitle]) < shortestTitleWords) { + shortestTitleWords = countingWords(allArticleTitles[articleTitle]); + shortestTitle = articleTitle; + } + } + + return allArticleTitles[shortestTitle]; +} + +function countingWords(string) { + let numberOfSpaces = 0 + let b = string.length + let character = 0 + while (character < b) { + if (string[character] === " ") { + numberOfSpaces++ + } + character++; + } + let numberOfWords = numberOfSpaces + 1; + return numberOfWords; } /* @@ -24,14 +62,34 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let arrayNumbers = []; + for (let article of allArticleTitles) { + if (containsNumbers(article) === true) { + arrayNumbers.push(article); + } + } + return arrayNumbers; } + function containsNumbers(str) { + return /\d/.test(str); +} + + + /* 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 sample = allArticleTitles.length + let sum = 0 + for (let article = 0; article < sample; article++) { + sum = sum + allArticleTitles[article].length; + } + return Math.round(sum / sample); + } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..f2fee3e9 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let averageArray = [] + for (let average of closingPricesForAllStocks) { + averageArray.push(getAverage(average)); + } + return averageArray +} + +function getAverage(pricesOfEachStock) { + let sum = 0; + for (let price of pricesOfEachStock) { + sum = sum + price + } + return Number((sum / 5).toFixed(2)); } /* @@ -49,8 +62,16 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let differenceArray = [] + for (let difference of closingPricesForAllStocks) { + differenceArray.push(Number((difference[difference.length-1] - difference[0]).toFixed(2))); // Definitely this is supercomplicated + } + return differenceArray; + } + + /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. Implement the below function, which @@ -65,6 +86,28 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let theHighestPrice = highestPrice(closingPricesForAllStocks); + console.log(theHighestPrice) + let report = []; + let i = 0; + for (let stock of stocks) { + report.push("The highest price of " + stock.toUpperCase() + " in the last 5 days was " + theHighestPrice[i]) + i++; + console.log(i); + } + return report; + +} + +function highestPrice(closingPrices) { + highestPrices = []; + for (let price of closingPrices) { + highestPrices.push(/*Number*/((Math.max.apply(null, price)).toFixed(2))) // Found this solution here: https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript + // It works but I don't understand it - If I kept the Number method it would show the last higestPrice with one decimal after the point; not two... + // highestPrices.push(Math.max(price)) This didn't work + } + return highestPrices; + console.log(highestPrices) } From 96d4db076ea263cae0c0b65c07a5fbd18587de8a Mon Sep 17 00:00:00 2001 From: George <108883931+GeorgePrimentas@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:57:49 +0000 Subject: [PATCH 2/2] Tiny refinement on 4th Mandatory exercise --- 2-mandatory/4-stocks.js | 1 - 1 file changed, 1 deletion(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index f2fee3e9..41d5338a 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -93,7 +93,6 @@ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { for (let stock of stocks) { report.push("The highest price of " + stock.toUpperCase() + " in the last 5 days was " + theHighestPrice[i]) i++; - console.log(i); } return report;