From e272997f0b5a1af1758b7ddc0537c6d67668b7fb Mon Sep 17 00:00:00 2001 From: Tersia Koetzee Date: Fri, 15 Jul 2022 13:06:03 +0200 Subject: [PATCH 1/4] Exercises done --- 1-exercises/A-undefined/exercise.js | 9 +++++---- 1-exercises/B-while-loop/exercise.js | 15 ++++++++++++++- 1-exercises/C-while-loop-with-array/exercise.js | 13 ++++++++++++- 1-exercises/D-do-while/exercise.js | 11 +++++++++++ 1-exercises/E-for-loop/exercise1.js | 14 +++++++++++--- 1-exercises/E-for-loop/exercise2.js | 13 +++++++++++++ 1-exercises/F-for-of-loop/exercise.js | 13 ++++++++++++- 7 files changed, 78 insertions(+), 10 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..62ddf1a5 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,12 +9,12 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 (answer) a is undefined because nothing has been assigned to it let a; console.log(a); -// Example 2 +// Example 2 (answer) the variable has not been returned in the function. function sayHello() { let message = "Hello"; } @@ -28,9 +28,10 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); +// (answer) there is no string when calling this function on line 32. -// Example 4 +// Example 4 (answer) there is not a third number in the array the array only has 0, 1, 2 in it. let arr = [1,2,3]; console.log(arr[3]); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..680e5617 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -4,11 +4,24 @@ Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. The list of numbers should start with 0. n is being passed in as a parameter. */ - +let limit = 10; +let n = 0; function evenNumbers(n) { // TODO + + let number = 0 + const numList = [] + while (number < n && n > 0){ + numList.push(number * 2) + number++ + } + return numList.join(',') + } +// console.log(evenNumbers(3)); +// console.log(evenNumbers(0)); +// console.log(evenNumbers(10)); 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..43b28aba 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,19 @@ const BIRTHDAYS = [ "November 15th" ]; + + +let birthdays = ""; +let i = 0; + function findFirstJulyBDay(birthdays) { // TODO +while (BIRTHDAYS[i]) { + birthdays = BIRTHDAYS[5] + i++; + +} +return birthdays; } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +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..d16fd159 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,17 @@ function evenNumbersSum(n) { // TODO + if (n === 0) return 0 + let x = 0 + test = 1 + listNum = 0 +do { + x += 2 + listNum += x + test++ +} +while (test < n) +return listNum } 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..141ad4ec 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -7,8 +7,16 @@ // 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++; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } + +for(let i = 65; i <= 90; i++){ + + console.log(String.fromCharCode(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..1f401c56 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,6 +26,19 @@ const AGES = [ 49 ]; +let test = [] + +// for (let index = 0; index < WRITERS.length; index++) { + +// for (let i = 0; i < AGES.length; i++) { +// test.push(WRITERS[index] + AGES[i]); +// console.log(test); +// } +// } + +for (let index = 0; index < WRITERS.length; index++) { + console.log(WRITERS[index] + " is " + AGES[index] + " years old "); +} // TODO - Write for loop code here /* diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..0d0e3694 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,18 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +let string = ""; +for (let index of tubeStations) { + string += index; + console.log(index); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +let sentence = ""; +for (let x of str) { + sentence += x; + x = x + ""; + console.log(x.toUpperCase()); +} From ec4e197c5d0ddd16d1e5b43943cdbf4172874035 Mon Sep 17 00:00:00 2001 From: Tersia Koetzee Date: Fri, 15 Jul 2022 14:37:24 +0200 Subject: [PATCH 2/4] mandatory --- 2-mandatory/1-weather-report.js | 68 ++++++++++++------------ 2-mandatory/2-retrying-random-numbers.js | 5 ++ 2-mandatory/3-financial-times.js | 15 ++++++ 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..01a8f92b 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,52 +12,50 @@ */ function getTemperatureReport(cities) { - // TODO + // TODO + let forCast = []; + for (each of cities) { + let temperature = temperatureService(each); + forCast.push(`The temperature in ${each} is ${temperature} degrees`); + } + return forCast; } - /* ======= 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); - - return temparatureMap.get(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); + + return temparatureMap.get(city); } test("should return a temperature report for the user's cities", () => { - 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" - ]); + 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", + ]); }); test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in Barcelona is 17 degrees", - "The temperature in Dubai is 27 degrees" - ]); + let usersCities = ["Barcelona", "Dubai"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in Barcelona is 17 degrees", + "The temperature in Dubai is 27 degrees", + ]); }); test("should return an empty array if the user hasn't selected any cities", () => { - expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file + expect(getTemperatureReport([])).toEqual([]); +}); diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..efeb553e 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 num = 0; + do { + num = generateRandomNumber(); + } while (num <= 50); + return num; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..b25d27e2 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,9 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + return allArticleTitles.filter((test) => { + if (test.length <= 65) return test; + }); } /* @@ -15,6 +18,12 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let words = allArticleTitles[0]; + for (let index = 0; index < allArticleTitles.length; index++) { + if (allArticleTitles[index].split(" ").length < words.split(" ").length) + words = allArticleTitles[index]; + } + return words; } /* @@ -24,6 +33,12 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + const amount = 0; + for (let heading of allArticleTitles) { + amount += heading.length; + } + let total = amount / allArticleTitles.length; + return Math.round(total); } /* From 0e43175034275cf41668b4ac41e1cd2cd7aadf17 Mon Sep 17 00:00:00 2001 From: Tersia Koetzee Date: Fri, 15 Jul 2022 20:37:06 +0200 Subject: [PATCH 3/4] mandatory updated --- 2-mandatory/3-financial-times.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index b25d27e2..63132823 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -18,12 +18,7 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO - let words = allArticleTitles[0]; - for (let index = 0; index < allArticleTitles.length; index++) { - if (allArticleTitles[index].split(" ").length < words.split(" ").length) - words = allArticleTitles[index]; - } - return words; + } /* @@ -33,12 +28,7 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO - const amount = 0; - for (let heading of allArticleTitles) { - amount += heading.length; - } - let total = amount / allArticleTitles.length; - return Math.round(total); + } /* From e1818f8d7a2e33ff86e45523ae05a7af0a39686e Mon Sep 17 00:00:00 2001 From: Tersia Koetzee Date: Fri, 29 Jul 2022 16:04:27 +0200 Subject: [PATCH 4/4] extra --- 3-extra/1-factorial.js | 7 +++++++ 3-extra/3-fibonacci.js | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..923d5761 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,6 +9,13 @@ */ function factorial(input) { + + let myTest = 1; + for (let index = input; index < 0; index -= 1) { + myTest *= index; + } + + return myTest // TODO } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..3ace6a67 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,6 +14,14 @@ */ function generateFibonacciSequence(n) { + const fibonacci = [0, 1]; + for (let i = n - 2; i > 0; i -= 1) { + fibonacci.push( + fibonacci[fibonacci.length - 2] + + fibonacci[fibonacci.length - 1] + ); + } + return fibonacci; // TODO }