From 062571d0b50a96429da338356666ce0cead3dcc2 Mon Sep 17 00:00:00 2001 From: baharbazargan62 <105979952+baharbazargan62@users.noreply.github.com> Date: Sun, 18 Sep 2022 12:59:58 +0100 Subject: [PATCH 1/2] exercises update --- 1-exercises/B-while-loop/exercise.js | 20 +++++++++++++++---- .../C-while-loop-with-array/exercise.js | 7 +++++++ 1-exercises/D-do-while/exercise.js | 9 ++++++++- 1-exercises/E-for-loop/exercise1.js | 5 ++--- 1-exercises/E-for-loop/exercise2.js | 4 +++- 1-exercises/F-for-of-loop/exercise.js | 16 ++++++++++----- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..59ba0fca 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,21 @@ */ function evenNumbers(n) { - // TODO + // TODO + // const arr =[]; + // for (let i=0;i<(n*2);i+=2){ + // arr.push(i); + // } + // return arr; + const arr = []; + let i = 0; + while (i < n * 2) { + arr.push(i); + i += 2; + } + return arr; } -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..a92acade 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,13 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i=0; + while (i Date: Fri, 23 Sep 2022 19:14:49 +0100 Subject: [PATCH 2/2] mandatory --- 1-exercises/F-for-of-loop/exercise.js | 21 +++++++------ 2-mandatory/1-weather-report.js | 6 ++++ 2-mandatory/2-retrying-random-numbers.js | 5 +++ 2-mandatory/3-financial-times.js | 39 ++++++++++++++++++++++-- 2-mandatory/4-stocks.js | 18 +++++++++++ 3-extra/1-factorial.js | 5 +++ 3-extra/2-array-of-objects.js | 8 +++++ 3-extra/3-fibonacci.js | 29 +++++++++++------- 8 files changed, 107 insertions(+), 24 deletions(-) diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index d4b68225..8f5aa990 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -4,19 +4,20 @@ // TODO Use a for-of loop to output each of the tube stations below. let tubeStations = [ - "Aldgate", - "Baker Street", - "Picadilly Circus", - "Oxford Street", - "Tottenham Court Road", + "Aldgate", + "Baker Street", + "Picadilly Circus", + "Oxford Street", + "Tottenham Court Road" ]; for (let x of tubeStations) { - console.log(x); -} + console.log(x) + } + // 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.toUpperCase()); -} +for (let x of str){ + console.log(x.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..fcafea14 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,15 @@ function getTemperatureReport(cities) { // TODO + let arr =[]; + for (let city of cities){ + arr.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); + } + return arr; } + /* ======= 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..c5d295d0 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let randomNumber =0; + do { + randomNumber = generateRandomNumber() + }while (randomNumber <= 50); + return randomNumber; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..be5f856c 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,8 +6,13 @@ */ function potentialHeadlines(allArticleTitles) { // TODO -} - + const arr = []; + for ( let x of allArticleTitles){ + if (x.length<= 65 ) { + arr.push(allArticleTitles); + } + return arr; + } /* 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,15 +20,37 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO +// let i = 0; +// let j; +// while (i < allArticleTitles.length) { +// j = i + 1; +// while (j < allArticleTitles.length) { +// if (allArticleTitles[j] < allArticleTitles[i]) { +// let tempStr = allArticleTitles[i]; +// allArticleTitles[i] = allArticleTitles[j]; +// allArticleTitles[j] = tempStr; +// } +// j++; +// } +// i++; +// } +// return allArticleTitles[0]; } + /* 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 + // const arr=[]; + // for (i=0 ;i { + return /\d/.test(element); + }); } /* @@ -32,6 +59,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let sum =0; + for ( let i=0 ;i0 ;i--){ + factorial*=i + } + return factorial; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..9dc70160 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,14 @@ function getHighestRatedInEachGenre(books) { // TODO + let arr=[]; + for(let i=0;i { - expect(generateFibonacciSequence(10)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - ); + expect(generateFibonacciSequence(10)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, + ]); }); test("should return the first 5 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(5)).toEqual( - [0, 1, 1, 2, 3] - ); + expect(generateFibonacciSequence(5)).toEqual([0, 1, 1, 2, 3]); }); test("should return the first 15 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(15)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] - ); -}); \ No newline at end of file + expect(generateFibonacciSequence(15)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, + ]); +});