diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..6c521e94 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,27 +10,41 @@ */ // Example 1 +//We are declaring a variable here, but never assigning it to a value - +//therefore the variable is undefined. let a; -console.log(a); +console.log(a); // There's no value assigned to variable a // Example 2 +//Explanation: Here, we are assigning the value returned from the function +// sayHello to the variable hello. +// But sayHello doesn't actually return anything, so hello is undefined. function sayHello() { - let message = "Hello"; + let message = "Hello"; // There's no output from this function. } let hello = sayHello(); -console.log(hello); +console.log(hello); //log 'hello', but due to sayHello()function is undefined, so it's logged undefined as well. // Example 3 +// Explanation: Here, we have a function which takes a parameter called user. +// But when we call the function, we're not passing in any arguments - there's nothing in the brackets! +// Because of this, the value of the user parameter inside the function is undefined. + function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); // There's no argument assigned when call sayHelloToUser()function. // Example 4 +// Explanation: Here, we are trying to retrieve an element from the array at index 3. +// But in this case, the array doesn't have any value at index 3 - +// it only has values at indexes 0, 1 and 2. +// Therefore, retrieving an element from index 3 will give us undefined. + let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); // There's no 4th element in array, so it logs 'undefined'. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8895bd6f 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -1,12 +1,25 @@ /* - while loops can be useful when you want to execute some code as long as some condition is true. + 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. - The list of numbers should start with 0. n is being passed in as a parameter. + 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 + // TODO + // initializing an array to hold even numbers: + let result = []; + let i = 0; + while (i < n) { + result.push(i * 2); + i++; + } + // joining the elements of the array together as a string + // Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join + console.log(result.join()) } evenNumbers(3); // should output 0,2,4 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..e75a4afb 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -1,7 +1,9 @@ /* Loops can be useful when working with arrays. - In the below example, imagine we've defined an array holding the birthdays of your closest friends. - Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function. + In the below example, imagine we've defined an array holding the birthdays + of your closest friends. + Use a while loop to search through the array until you find the first + birthday in July, then return that birthday from the function. */ const BIRTHDAYS = [ @@ -18,6 +20,21 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i=0; + while(i x.includes('July')); + // return newBday; + // i++; + // + + //or using startsWith()method: + if(BIRTHDAYS[i].startsWith('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..11e79004 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -1,15 +1,55 @@ /* - Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop. - - If the condition in a while loop is initially false, the body of the loop will never execute - - But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once + Sometimes when using loops, we'll want to execute the body of the loop + at least once. We can make sure this happens by using a do-while loop. + - If the condition in a while loop is initially false, the body of + the loop will never execute + - But in a do-while loop, because the condition is checked after + the body, we know that it will always execute at least once - Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) + Using a do-while loop, write a function which returns the sum of the + first n even numbers (starting from 0) */ function evenNumbersSum(n) { - // TODO -} + // TODO + + //1.own solution: initialize first even number is 0, sum is 0, counter from 0; + // let currEvenNumber = 0; + // let sum = 0; + // let i = 0; + // do { + // //sum of first n even number(0): + // sum += currEvenNumber; + // //next even number: + // currEvenNumber += 2; + // //next counter: + // i++; + // } while (i < n); //condition + // return sum; //return final output when condition is false. + +//2.solution from google classroom review: + let sum = 0; + let i=0; + do { + sum += (i * 2); + i++; + }while(i < n); + return sum; +} +// for loop: solution +// let currEvenNumber = 0, +// sum = 0; +// // sum of first n even numbers +// for (let i = 0; i < n; i++) { +// sum += currEvenNumber; +// // next even number +// currEvenNumber += 2; +// } +// // required sum +// 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 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..445ca5f3 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,14 @@ // 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++; +//} + +for (let i=0; i< 26; i++){ + console.log((String.fromCharCode(97 + i)).toUpperCase()); } + // 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..2980a4f3 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -1,13 +1,16 @@ /* - for loops can be useful when we already know exactly how many times we want to loop. + for loops can be useful when we already know exactly how many times + we want to loop. Below we have 2 arrays which have exactly the same number of values. - The first array is a list of writers - The second array is a list of ages - The writers in the first array correspond with the ages in the second array. + The writers in the first array correspond with the ages in the second + array. For example, Virginia Woolf is 59 years old. - Using a for loop, output to the console a line about the age of each writer. + Using a for loop, output to the console a line about the age of each + writer. */ const WRITERS = [ @@ -27,7 +30,23 @@ const AGES = [ ]; // TODO - Write for loop code here + //1.own solution: +//for (let i=0, j=0; i< WRITERS.length, j< AGES.length;i++, j++){ +// console.log(`${WRITERS[i]} is ${AGES[j]} years old`); +//} + //2. solution from google classroom review: + for (let i=0; i 50. + In the below example, we want to keep calling generateRandomNumber until + we get a value that is > 50. Implement this using a do-while loop. */ @@ -10,7 +11,12 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + // TODO - implement using a do-while loop + let x; + do { + x = generateRandomNumber(); + } while (x <= 50); //keep trying until we get a number that is > 50 + return x; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..e56fc809 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,32 +6,125 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + +//1.filter()method +return allArticleTitles.filter(letters => letters.length<=65); + +//2.for ... of loop +// let headlines = []; +// for(let title of allArticleTitles){ +// if (title.length <=65){ +// headlines.push(title); +// } +// return headlines +// } + } /* 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) + Implement the function below, which returns the title with the fewest + words. + (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + // TODO: + //Barath's solution: + // declare a function in advance to return the number of every element in the array: + + + // function numberOfWords(title){ + // return title.split(' ').length; + // } + + // function titleWithFewestWords(allArticleTitles) { + // let lowestNumberOfWords; + // let titleWithFewestWords; + + // for (let title of allArticleTitles) { + // // working out the number of words in the title by splitting on the space + // // character + // // this will generate an array. Read more: + // //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split + // let numWords = numberOfWords.title(" ").length; + + // if ( + // lowestNumberOfWords === undefined || + // numWords < lowestNumberOfWords + // ) { + // lowestNumberOfWords = numWords; + // titleWithFewestWords = title; + // } + // } + + // return titleWithFewestWords; + // } + + + let wordNums =[]; + for (let i=0; i=0){ + NumberArray.push(allArticleTitles[i]) + } + } + return NumberArray } +//2. split two parts to solve problem: + //1) Creating another function to help break this problem down into smaller parts +// function doesTitleContainANumber(title) { +// for(let character of title) { +// if(character >= '0' && character <= '9') { +// return true; +// } +// } +// return false; +// } +// // 2) make use of the function created above: +// let articlesWithNumbers = []; + +// for(let title of allArticleTitles) { +// if(doesTitleContainANumber(title)) { +// articlesWithNumbers.push(title); +// } +// } +// return articlesWithNumbers; +// } + /* The Financial Times wants to understand what the average number of characters in an article title is. Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { // TODO + + let sum = 0; + + for (let i=0; i { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..c7ae1409 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -1,15 +1,35 @@ /* - 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). + 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. + Using a loop, complete the function below so it returns the factorial of + the number being passed in. */ function factorial(input) { // TODO + //for loop: + // let facNum = 1; + // for (; input > 0 ; input-- ){ + // facNum *= input; + // } + // return facNum + + //do while: + let result = 1; + + let i = input; + do { + result *= i; + i--; + } while (i > 0); + + return result; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..030cdf7c 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,8 +12,42 @@ function getHighestRatedInEachGenre(books) { // TODO -} +// const result = books.reduce((acc, cur) => { +// const groupByGenre = cur.genre; +// if (!acc[groupByGenre]) { +// acc[groupByGenre] = []; +// } +// acc[groupByGenre].push(cur); +// return acc; +// }, {}); +// const genreName = Object.keys(result); +// const arr = []; +// for (let i = 0; i < genreName.length; i++) { +// arr.push(result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title); +// } +// return arr; +// } + + //solution from google classroom review: + let highestRated = {}; + + for (let book of books) { + // if this is the first time we're seeing this genre OR the rating we've seen is not as high as the current book + if ( + highestRated[book.genre] === undefined || + highestRated[book.genre].rating < book.rating + ) { + // then this book is now the highest rated in the genre so far + highestRated[book.genre] = book; + } + } + + // Here we just want to get the highest rated books (the values of the object) + // and then get the title for each one + return Object.values(highestRated).map((book) => book.title); +} + /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ @@ -69,7 +103,6 @@ const BOOKS = [ }, ] - /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..4aeaa7cb 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -1,5 +1,6 @@ /* - The Fibonacci Sequence is a famous sequence of numbers. Read more here: https://www.mathsisfun.com/numbers/fibonacci-sequence.html + 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. @@ -14,9 +15,54 @@ */ function generateFibonacciSequence(n) { - // TODO + // TODO + // for loop: + // let fibonacciNumArr=[]; + // const first2Num = [0,1]; + // for(let i=0; i { expect(generateFibonacciSequence(10)).toEqual(