From ac9f917a26104cd16405a2a63696bf69a5ca4d28 Mon Sep 17 00:00:00 2001 From: leilafarsani <108967493+leilafarsani@users.noreply.github.com> Date: Tue, 6 Dec 2022 13:30:08 +0000 Subject: [PATCH 1/2] Exercises and Mandatory! --- 1-exercises/A-undefined/exercise.js | 6 ++++ 1-exercises/B-while-loop/exercise.js | 11 ++++++- .../C-while-loop-with-array/exercise.js | 27 ++++++++++------ 1-exercises/D-do-while/exercise.js | 8 ++++- 1-exercises/E-for-loop/exercise1.js | 7 +++- 1-exercises/E-for-loop/exercise2.js | 5 ++- 1-exercises/F-for-of-loop/exercise.js | 7 ++++ 2-mandatory/1-weather-report.js | 3 +- 2-mandatory/2-retrying-random-numbers.js | 8 ++++- 2-mandatory/3-financial-times.js | 32 +++++++++++++++++-- 2-mandatory/4-stocks.js | 26 ++++++++++++++- 11 files changed, 120 insertions(+), 20 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..fe7b1edb 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,15 +13,18 @@ let a; console.log(a); +// The variable "a" is declared but no value is assigned to it. // Example 2 function sayHello() { let message = "Hello"; } + let hello = sayHello(); console.log(hello); +// The function doesn't return a value. // Example 3 function sayHelloToUser(user) { @@ -30,7 +33,10 @@ function sayHelloToUser(user) { sayHelloToUser(); +// No value is passed for the function parameter "user". // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// There is no value for array fourth element. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..2692fd81 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,7 +7,16 @@ function evenNumbers(n) { // TODO -} + let i = 0; + let even = []; + while (i < 2 * n) { + even.push(i); + i += 2; + } + return even.toString(); + } + console.log(evenNumbers()); + evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..29868c9d 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -5,19 +5,26 @@ */ const BIRTHDAYS = [ - "January 7th", - "February 12th", - "April 3rd", - "April 5th", - "May 3rd", - "July 11th", - "July 17th", - "September 28th", - "November 15th" + "January 7th", + "February 12th", + "April 3rd", + "April 5th", + "May 3rd", + "July 11th", + "July 17th", + "September 28th", + "November 15th", ]; function findFirstJulyBDay(birthdays) { - // TODO + // TODO + let i = 0; + while (i < birthdays.length) { + if (birthdays[i][1] === "u") { + 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..82bd698f 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 +// TODO + let i = 0; + let sum = n * (n - 1); + do { + return sum; + i++; + } while (i < 5); } 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..33b11780 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,14 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; +/*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)); } + // 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..a5e7e82b 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,10 @@ const AGES = [ ]; // TODO - Write for loop code here - +for (i = 0; i < WRITERS.length; i++) { + let text = WRITERS[i] + " is " + AGES[i] + " years old"; + console.log(text); +} /* 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..50ccdb7d 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let tube of tubeStations){ + console.log(tube); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (let x of str){ + console.log(x.toLocaleUpperCase()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..87f240f7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,8 @@ function getTemperatureReport(cities) { // TODO + return cities.map(el => "The temperature in " + el + " is " + temperatureService(el) + " degrees"); } - - /* ======= 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..a0a2c4ce 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,8 +10,14 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + // TODO - implement using a do-while loop + let n; + do { + n = generateRandomNumber(); + } while (n <= 50); + return n; } +console.log(getRandomNumberGreaterThan50()); /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..2cb005b4 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,9 +5,15 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + // TODO + let articleArr = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + articleArr.push(allArticleTitles[i]); + } + } + return articleArr; } - /* 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. @@ -15,6 +21,13 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let wordsCount= []; + for (let i = 0; i < allArticleTitles.length; i++){ + wordArticle = allArticleTitles[i].split(""); + wordsCount.push(wordArticle.length); + } + let index = wordsCount.indexOf(Math.min(...wordsCount)) + return allArticleTitles[index]; } /* @@ -24,6 +37,14 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let numArticle = []; + let pattern =/[0-9]/g; + for (let i=0; i Date: Tue, 6 Dec 2022 21:11:00 +0000 Subject: [PATCH 2/2] Extra Done! --- 3-extra/1-factorial.js | 11 +++++++++++ 3-extra/2-array-of-objects.js | 27 ++++++++++++++++++++++++++- 3-extra/3-fibonacci.js | 5 +++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..602c8696 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,17 @@ function factorial(input) { // TODO + let result = 1; + let i = input; + if (input = 0) { + return 1; + } else { + while (i > 0){ + result *= i; + i--; + } + return result; + } } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..938fee3c 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,7 +12,32 @@ function getHighestRatedInEachGenre(books) { // TODO -} + let childrenGenre = []; + let nonFictionGenre = []; + let cookingGenre = []; + let booksArr = []; + for (let i = 0; i < books.length; i++) { + if (books[i].genre === "children") { + childrenGenre.push(books[i]); + } else if (books[i].genre === "non-fiction") { + nonFictionGenre.push(books[i]); + } else { + cookingGenre.push(books[i]); + } + } + let childrenSorted = childrenGenre.sort((a, b) => a.rating - b.rating); + let nonFictionSorted = nonFictionGenre.sort((a, b) => a.rating - b.rating); + let cookingSorted = cookingGenre.sort((a, b) => a.rating - b.rating); + + + booksArr.push(childrenSorted[childrenSorted.length - 1].title); + booksArr.push(cookingSorted[cookingSorted.length - 1].title); + booksArr.push(nonFictionSorted[nonFictionSorted.length - 1].title); + + return booksArr; +} + + /* ======= Book data - DO NOT MODIFY ===== */ diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..0aa967b6 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,11 @@ function generateFibonacciSequence(n) { // TODO + let fibSeq = [0, 1]; + for (let i = 2; i < n; i++) { + fibSeq.push(fibSeq[i - 1] + fibSeq[i - 2]); + } + return fibSeq; } /* ======= TESTS - DO NOT MODIFY ===== */