diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..422c09ab 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,7 +12,7 @@ // Example 1 let a; console.log(a); - +//Answer: a is not defined. // Example 2 function sayHello() { @@ -21,12 +21,13 @@ function sayHello() { let hello = sayHello(); console.log(hello); - +//Answer: No value returned by the function. // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); } +//Answer:The variables haven't been declared. sayHelloToUser(); @@ -34,3 +35,4 @@ sayHelloToUser(); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//Answer:Zero index, meaning that we only have up to index 2. Index position 3 does not exist. \ 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..242e32af 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -1,14 +1,25 @@ /* while loops can be useful when you want to execute some code as long as some condition is true. - Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. + Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-separated string. The list of numbers should start with 0. n is being passed in as a parameter. */ function evenNumbers(n) { - // TODO + let array = [] + let i = 0; + while (i < n) { + array.push(i * 2); + i++ + } + console.log(array.toString()); } + + 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..9fcf8cdd 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,13 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < birthdays.length) { + if (birthdays[i].includes("July")){ + 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..bbfc496e 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 +let sum = 0; +let i = 0; //starting from zero +do { + sum = i * (i + 1); //formula for the sum of consecutive even numbers: n(n+1) or JSshort: sum+= i * 2 +i++; +} while (i < n); +return sum; } 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..b964775b 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,11 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +//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)); - 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..2c15d29b 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -25,9 +25,9 @@ const AGES = [ 63, 49 ]; - -// 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: diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..eebc3a08 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,14 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; +for (let tubeStation of tubeStations) { + console.log(tubeStation); + } -// TODO Use a for-of loop to capitalise and output each letter in the string seperately. +// TODO Use a for-of loop to capitalize and output each letter in the string separately. //Separately = different line? let str = "codeyourfuture"; + +for (let char of str) { +console.log(char.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..a7a455d0 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -2,7 +2,7 @@ Imagine we're making a weather app! We have a list of cities that the user wants to track. - We also already have a temperatureService function which will take a city as a parameter and return a temparature. + We also already have a temperatureService function which will take a city as a parameter and return a temperature. Implement the function below: - take the array of cities as a parameter @@ -12,24 +12,31 @@ */ function getTemperatureReport(cities) { - // TODO -} + let citiesTemp = []; + for (city of cities) { + let temp = temperatureService(city); + citiesTemp.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); + } + return citiesTemp; + } + + /* ======= 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 temperatureMap = new Map(); + + temperatureMap.set('London', 10); + temperatureMap.set('Paris', 12); + temperatureMap.set('Barcelona', 17); + temperatureMap.set('Dubai', 27); + temperatureMap.set('Mumbai', 29); + temperatureMap.set('São Paulo', 23); + temperatureMap.set('Lagos', 33); - return temparatureMap.get(city); + return temperatureMap.get(city); } test("should return a temperature report for the user's cities", () => { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..47f32864 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,7 +11,13 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop -} + //let value; + do { + value = generateRandomNumber(); + } while( value <= 50) + return value; + } + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..692f4aa6 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,20 +5,27 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} + let conformingTitles = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length < 65) { + conformingTitles.push(allArticleTitles[i]); + } + } + return conformingTitles; + } + /* 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. - (you can assume words will always be seperated by a space) + (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + } /* - The editor of the FT has realised that headlines which have numbers in them get more clicks! + The editor of the FT has realized 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) */