From f83e237d6ceb3b59cf01e7500c3ab5c75b7dc713 Mon Sep 17 00:00:00 2001 From: MohammadSefidgar <108987757+MohammadSefidgar@users.noreply.github.com> Date: Thu, 8 Dec 2022 12:48:35 +0000 Subject: [PATCH] 1-exercises --- 1-exercises/A-undefined/exercise.js | 4 +- 1-exercises/B-while-loop/exercise.js | 11 +++-- .../C-while-loop-with-array/exercise.js | 11 ++++- 1-exercises/D-do-while/exercise.js | 7 ++++ 1-exercises/E-for-loop/exercise1.js | 7 +++- 1-exercises/E-for-loop/exercise2.js | 21 +++++----- 1-exercises/F-for-of-loop/exercise.js | 9 +++- 2-mandatory/1-weather-report.js | 42 ++++++++++++------- 8 files changed, 76 insertions(+), 36 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..cbfbde91 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -32,5 +32,5 @@ sayHelloToUser(); // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1, 2, 3]; +console.log(arr[3]); \ 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..9c1f49ed 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,8 +7,13 @@ function evenNumbers(n) { // TODO + let i = 0; + while (i < n) { + console.log(i * 2); + i++ + } } -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 +// 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 \ No newline at end of file diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..23142691 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -13,11 +13,18 @@ const BIRTHDAYS = [ "July 11th", "July 17th", "September 28th", - "November 15th" + "November 15th", ]; function findFirstJulyBDay(birthdays) { // TODO + i = 0; + while (i < birthdays.lenght) { + if (birthdays[i].includes("July")) { + return birthdays[i]; + } + i++ + } } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" \ No newline at end of file diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..4acd5aee 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,13 @@ function evenNumbersSum(n) { // TODO + let result = ""; + let i = 0; + do { + i += 1; + result += '${n * (n - 1)} '; + } while (i < 1); + return result; } 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..422e4592 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -7,8 +7,11 @@ // Change the below code to use a for loop instead of a while loop. let i = 0; -while(i < 26) { +while (i < 26) { console.log(String.fromCharCode(97 + i)); i++; + for (i = 0; i < 26; i++) { + console.log(String.fromCharCode(97 + i)); + } } -// The output shouldn't change. +// 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..06e720bc 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -15,17 +15,18 @@ const WRITERS = [ "Zadie Smith", "Jane Austen", "Bell Hooks", - "Yukiko Motoya" -] - -const AGES = [ - 59, - 40, - 41, - 63, - 49 + "Yukiko Motoya", ]; +const AGES = [59, 40, 41, 63, 49]; + +// TODO - Write for loop code here +for (i = 0; i < WRITERS.length; i++) { + console.log(`${WRITERS[i]} is ${AGES[i]} years old`); +} + +/* + // TODO - Write for loop code here /* @@ -36,4 +37,4 @@ Zadie Smith is 40 years old Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old -*/ +*/ \ No newline at end of file diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..d43a4e20 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -8,9 +8,14 @@ let tubeStations = [ "Baker Street", "Picadilly Circus", "Oxford Street", - "Tottenham Court Road" + "Tottenham Court Road", ]; - +for (let stations of tubeStations) { + console.log(stations); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let charCaps of str) { + console.log(charCaps.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..b7fa7ba0 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,31 +13,43 @@ function getTemperatureReport(cities) { // TODO + let cityTemp = []; + for (let i = 0; i < cities.length; i++) { + cityTemp.push( + `The temperature in ${cities[i]} is ${temperatureService( + cities[i] + )} degrees` + ); + } + return cityTemp; } /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); - - temparatureMap.set('London', 10); - temparatureMap.set('Paris', 12); - temparatureMap.set('Barcelona', 17); - temparatureMap.set('Dubai', 27); - temparatureMap.set('Mumbai', 29); - temparatureMap.set('São Paulo', 23); - temparatureMap.set('Lagos', 33); - + let temparatureMap = new Map(); + + temparatureMap.set("London", 10); + temparatureMap.set("Paris", 12); + temparatureMap.set("Barcelona", 17); + temparatureMap.set("Dubai", 27); + temparatureMap.set("Mumbai", 29); + temparatureMap.set("São Paulo", 23); + temparatureMap.set("Lagos", 33); + return temparatureMap.get(city); } test("should return a temperature report for the user's cities", () => { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] + let usersCities = ["London", "Paris", "São Paulo"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in London is 10 degrees", + "The temperature in Paris is 12 degrees", + "The temperature in São Paulo is 23 degrees", + ]); + expect(getTemperatureReport(usersCities)).toEqual([ "The temperature in London is 10 degrees",