From 78eee66ca75405a885f9d434a533431e95d55640 Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Tue, 20 Sep 2022 15:34:35 +0100 Subject: [PATCH 1/4] done --- 1-exercises/A-undefined/exercise.js | 13 ++++++------ 1-exercises/B-while-loop/exercise.js | 20 +++++++++++++++++-- .../C-while-loop-with-array/exercise.js | 12 ++++++++--- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..4154cf94 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,26 +11,27 @@ // Example 1 let a; -console.log(a); - +console.log +// Answer 1:Becuase no value was given to console.log // Example 2 function sayHello() { let message = "Hello"; } - let hello = sayHello(); console.log(hello); - +// Answer: for the reason the functin sayHello does not return any value. +// and when call it , we recieve undifined. // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); } - sayHelloToUser(); +//Answer: becuase the first argumant does not assign a value.( in line 30) // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//Answer: there is no index 3 in the array (in line 35) \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..207da7f6 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,25 @@ */ function evenNumbers(n) { - // TODO -} + let output =''; // '' === false + let counter = 0; + + while( counter < n ){ + if ( counter == 0 ){ + output = "0" + }else{ '' + output = `${output},${(counter * 2)}` //output + "," + (counter * 2) + } + counter++; + } + if( output ){ + console.log( output ); + }else{ + console.log("should output nothing"); + } +} + 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..9e61df8d 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -14,10 +14,16 @@ const BIRTHDAYS = [ "July 17th", "September 28th", "November 15th" -]; +]; function findFirstJulyBDay(birthdays) { - // TODO + let counter = 0; + while ( counter < birthdays.length ) { + if( birthdays[counter].includes("July") ){ + return birthdays[counter]; + } + counter++; + } } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log( findFirstJulyBDay(BIRTHDAYS) ); // should output "July 11th" From 59b9a07f423984bd03a9a64b96856faab9e761a4 Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Wed, 21 Sep 2022 18:08:22 +0100 Subject: [PATCH 2/4] exercise D_E --- 1-exercises/D-do-while/exercise.js | 11 ++++++++--- 1-exercises/E-for-loop/exercise1.js | 8 ++++---- 1-exercises/E-for-loop/exercise2.js | 8 +++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..29603177 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,14 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + let counter = 0; + do { + sum += counter * 2; + counter++; + } while ( counter < n ); + return sum; } - -console.log(evenNumbersSum(3)); // should output 6 +console.log(evenNumbersSum(3)); // should output 6 console.log(evenNumbersSum(0)); // should output 0 console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..16f5b35a 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) { - 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. + \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..a86cd42c 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,13 +27,11 @@ const AGES = [ ]; // 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: -Virginia Woolf is 59 years old -Zadie Smith is 40 years old -Jane Austen is 41 years old -Bell Hooks is 63 years old Yukiko Motoya is 49 years old */ From 63ebdc54b487ca0b732bfc0042a0138dca70b61f Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Wed, 21 Sep 2022 22:28:40 +0100 Subject: [PATCH 3/4] F-exercise-done --- 1-exercises/F-for-of-loop/exercise.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..355233c4 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,15 @@ let tubeStations = [ "Tottenham Court Road" ]; - +for(let words of tubeStations) { + console.log(words) +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +let capitalise = ""; +for (let letter of str) { + capitalise += letter.toUpperCase(); + console.log(letter.toUpperCase()); +} +console.log("_____________________"); +console.log(capitalise); From 3f02da22269924ea6720105b0d4778376d587cae Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Wed, 28 Sep 2022 14:47:28 +0200 Subject: [PATCH 4/4] final --- 2-mandatory/1-weather-report.js | 11 ++++++-- 2-mandatory/2-retrying-random-numbers.js | 6 +++- 2-mandatory/3-financial-times.js | 35 +++++++++++++++++++++--- 2-mandatory/4-stocks.js | 27 ++++++++++++++++-- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..4a7bf68b 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,8 +11,15 @@ - Hint: you can call the temperatureService function from your function */ -function getTemperatureReport(cities) { - // TODO +function getTemperatureReport( cities ) { + let result = []; + for( let city of cities ){ + let temperature = temperatureService(city); + if ( temperature ){ + result.push( `The temperature in ${city} is ${temperature} degrees`); + } + } + return result; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..968d4f30 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,11 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let random; + do{ + random = generateRandomNumber() + }while( random < 50 ); + return random; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..ba34dbee 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -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 articles = []; + for (let item of allArticleTitles ){ + if (item.length <= 65 ){ + articles.push(item); + } + } + return articles; } /* @@ -14,7 +20,16 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestCount; + let fewestSentence; + for(let title of allArticleTitles) { + let curentCount = title.split(' ').length; + if( fewestCount === undefined || curentCount < fewestCount) { + fewestCount = curentCount; + fewestSentence = title; + } + } + return fewestSentence; } /* @@ -23,7 +38,15 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let article = []; + for( let item of allArticleTitles ){ + for( let string of item ){ + if ( string >= '0' && string <= '9' ){ + article.push( item ); + } + } + } + return article; } /* @@ -31,7 +54,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let total = 0; + for(let title of allArticleTitles) { + total += title.length; + } + return Math.round(total / allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..cf4ba9b7 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let avg = []; + for(let item of closingPricesForAllStocks ){ + let sum = 0; + for(let index of item ){ + sum += index; + } + avg.push( Math.round( sum / item.length * 100 ) / 100 ); + } + return avg; } /* @@ -48,7 +56,11 @@ 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 changes = []; + for( let item of closingPricesForAllStocks ){ + changes.push( Math.round( ( item[item.length - 1] - item[0] ) * 100 ) / 100 ); + } + return changes; } /* @@ -64,7 +76,16 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highest = []; + + for( let index in stocks ){ + let item = closingPricesForAllStocks[index]; + item.sort(function(a , b){ + return b - a; + }); + highest.push(`The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${item[0].toFixed(2)}`); + } + return highest; }