diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..c1200c80 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,25 +12,27 @@ // Example 1 let a; console.log(a); - +// a has no value assigned // Example 2 function sayHello() { let message = "Hello"; } +// the function sayHello is not returned let hello = sayHello(); console.log(hello); - +// the function is not returning anything // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); } - +// the user parameter does not pass anything sayHelloToUser(); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// there is no index 3 in the array \ 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..cf4ca85b 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,6 +6,14 @@ */ function evenNumbers(n) { + const arr = [] + let i = 0 + + while (i < n) { + arr.push(i * 2); + i++; + } + console.log(arr.join(', ')); // TODO } diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..9d08be2f 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,6 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { + let i = 0; + + while (i < birthdays.length - 1) { + let birthdayCheck = birthdays[i]; + if (birthdayCheck.startsWith("July")) { + return birthdayCheck; + } + i++; + } // TODO } diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..31e19082 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,14 @@ function evenNumbersSum(n) { // TODO + let sumNum = 0; + let i = 0; + + do { + sumNum = sumNum + i * 2; + i++; + } while (i < n); + return sumNum; } 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..41b9c720 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,12 @@ // 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++; -} +// let i = 0; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } // The output shouldn't change. +for (let i = 0; i < 26 ; i++) { + console.log(String.fromCharCode(97 + i)); +} \ 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..a4b4b1a4 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,9 @@ 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..a194fb85 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,12 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (const station of tubeStations) { + console.log(station.toUpperCase()); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (const letter of str) { + console.log(letter.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..dcc7e650 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,12 @@ function getTemperatureReport(cities) { // TODO + let arrTemp = []; + + for (let city of cities) { + arrTemp.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); + } + return arrTemp; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..814ae89e 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,8 +11,15 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let number = 0; + do { + number = generateRandomNumber(); + } while (number <= 50); + + return number; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("Returned value should always be greater than 50", () => { diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..c5bf8eee 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,15 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + + let titles = []; + + for (const title in allArticleTitles) { + if (titles.length <= 65) { + titles.push(title); + } + } + return titles; } /* @@ -15,6 +24,17 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let fewWords = allArticleTitles[0]; + let words = allArticleTitles[0].split(" ").length; + + for (let i = 0; i < allArticleTitles.length; i++) { + let theWordArrapy = allArticleTitles[i].split(" ").length; + if (theWordArray < words) { + fewWords = allArticleTitles[i]; + } + words = theWordArrapy; + } + return fewWords; } /* @@ -24,6 +44,7 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + } /* diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js deleted file mode 100644 index 31f8052c..00000000 --- a/3-extra/1-factorial.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - In maths, the factorial of an integer (written as n!) is the product of an integer, and all the integers below it (not including zero). - See: https://en.wikipedia.org/wiki/Factorial - For example, - 3! is 6 (because 3 * 2 * 1 = 6) - 5! is 120 (because 5 * 4 * 3 * 2 * 1 = 120) - - Using a loop, complete the function below so it returns the factorial of the number being passed in. -*/ - -function factorial(input) { - // TODO -} - -/* ======= TESTS - DO NOT MODIFY ===== */ - -test("3! should be 6", () => { - expect(factorial(3)).toEqual(6); -}); - -test("5! should be 120", () => { - expect(factorial(5)).toEqual(120); -}); - -test("10! should be 3628800", () => { - expect(factorial(10)).toEqual(3628800); -}); diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js deleted file mode 100644 index ee57960f..00000000 --- a/3-extra/2-array-of-objects.js +++ /dev/null @@ -1,82 +0,0 @@ -/* - This exercise includes an array of objects. You can read more about objects here: https://javascript.info/object - - Imagine you're working for an online store selling books (like Amazon). - Below, we have an array of book objects. - Each object contains the title of the book, the genre, and a rating based on user reviews. - - We want to find the title of the highest rated book in each genre to showcase on our home page. - Implement a function which takes the array of books as a parameter, and returns an array of book titles. - Each title in the resulting array should be the highest rated book in its genre. -*/ - -function getHighestRatedInEachGenre(books) { - // TODO -} - - -/* ======= Book data - DO NOT MODIFY ===== */ -const BOOKS = [ - { - title: "The Lion, the Witch and the Wardrobe", - genre: "children", - rating: 4.7 - }, - { - title: "Sapiens: A Brief History of Humankind", - genre: "non-fiction", - rating: 4.7 - }, - { - title: "Nadiya's Fast Flavours", - genre: "cooking", - rating: 4.7 - }, - { - title: "Harry Potter and the Philosopher's Stone", - genre: "children", - rating: 4.8 - }, - { - title: "A Life on Our Planet", - genre: "non-fiction", - rating: 4.8 - }, - { - title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", - genre: "cooking", - rating: 4.85 - }, - { - title: "Gangsta Granny Strikes Again!", - genre: "children", - rating: 4.9 - }, - { - title: "Diary of a Wimpy Kid", - genre: "children", - rating: 4.6 - }, - { - title: "BOSH!: Simple recipes. Unbelievable results. All plants.", - genre: "cooking", - rating: 4.6 - }, - { - title: "The Book Your Dog Wishes You Would Read", - genre: "non-fiction", - rating: 4.85 - }, -] - - -/* ======= TESTS - DO NOT MODIFY ===== */ -test("should return the highest rated book in each genre", () => { - expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( - [ - "The Book Your Dog Wishes You Would Read", - "Gangsta Granny Strikes Again!", - "Dishoom: The first ever cookbook from the much-loved Indian restaurant" - ] - )); -}); \ No newline at end of file diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js deleted file mode 100644 index 9ef9aec7..00000000 --- a/3-extra/3-fibonacci.js +++ /dev/null @@ -1,37 +0,0 @@ -/* - The Fibonacci Sequence is a famous sequence of numbers. Read more here: https://www.mathsisfun.com/numbers/fibonacci-sequence.html - - The sequence starts with 0 and 1. Each number after that, is the sum of the previous 2 numbers in the sequence. - So the third number in the sequence is 1, because 0 + 1 = 1. - Now we have 0, 1, 1. - The fourth number in the sequence is 2, because 1 + 1 = 2. (the last 2 numbers in the sequence were 1 and 1) - Now we have 0, 1, 1, 2. - The fifth number in the sequence is 3, because 1 + 2 = 3. (the last 2 numbers in the sequence were 1 and 2) - And so on... If we go a bit further the sequence looks like 0, 1, 1, 2, 3, 5, 8, 13,.... - - Can you implement a function that will generate the first n numbers in this sequence (starting with 0 and 1)? - - the input n will be passed in to the function as an argument, and will be a number greater than 2 -*/ - -function generateFibonacciSequence(n) { - // TODO -} - -/* ======= TESTS - DO NOT MODIFY ===== */ -test("should return the first 10 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(10)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - ); -}); - -test("should return the first 5 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(5)).toEqual( - [0, 1, 1, 2, 3] - ); -}); - -test("should return the first 15 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(15)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] - ); -}); \ No newline at end of file