diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..e3ee07d5 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,13 +10,13 @@ */ // Example 1 -let a; +let a; // variable a hasn't assigned the value console.log(a); // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; // there is no return value inside the function } let hello = sayHello(); @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //there is no argument // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //index 3 doesn't exit diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8ce20116 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,8 +6,16 @@ */ function evenNumbers(n) { - // TODO -} + let arr = []; + let counter = 0; + let evenNum = 0; + while (counter < n) { + arr.push(evenNum); + evenNum += 2; + counter ++; + } + console.log(arr.join()); + } evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..79a1ac80 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,15 @@ const BIRTHDAYS = [ "November 15th" ]; -function findFirstJulyBDay(birthdays) { - // TODO -} + function findFirstJulyBDay(birthdays) { + let i = 0; + while(i < birthdays.length){ + if (birthdays[i].includes("July")) { + return birthdays[i]; + } + i++; + } + } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +//console.log(BIRTHDAYS[5]); diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..3217be51 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,20 @@ */ function evenNumbersSum(n) { - // TODO + let counter = 0; + let sum = 0; + let evenNum = 0; + do { + sum += evenNum; + evenNum += 2; + counter ++; + + } while(counter < n); + + return sum; } 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 +console.log(evenNumbersSum(10)); // should output 90 +console.log(evenNumbersSum(20)); \ 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..63f77dad 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,8 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { + +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..38d1f6c4 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,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: diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..a732024d 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let x of tubeStations){ + 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()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..a24fb80f 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let arr = []; + for (let i=0; i 50){ + return randomNum; + } + i++; + } while(i < 51) + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..342480bf 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,8 +5,14 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} + let arr = []; + for (let i = 0; i < allArticleTitles.length; i++){ + if(allArticleTitles[i] <= 65){ + arr.push(allArticleTitles[i]); + } + } + return arr; + } /* The editor of the FT likes short headlines with only a few words! @@ -23,7 +29,13 @@ 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 arr = []; + for (let i = 0; i < allArticleTitles; i++){ + if(allArticleTitles.includes()){ + + } + return arr; + } } /* @@ -31,7 +43,7 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..9273789b 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,14 @@ */ function factorial(input) { - // TODO + if (input === 0 || input === 1){ + return 1; + } + + for (let i = input - 1; i >= 1; i--){ + input *= i; + } + return input; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..1c384160 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,12 @@ */ function generateFibonacciSequence(n) { - // TODO + const fib = [0, 1]; + + for (let i = 2; i < n; i++) { + fib[i] = fib[i-1] + fib[i-2]; + } + return fib; } /* ======= TESTS - DO NOT MODIFY ===== */