diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..9219896b 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,9 +12,10 @@ // Example 1 let a; console.log(a); +// value not assigned -// Example 2 +// Example 2 message not assigned function sayHello() { let message = "Hello"; } @@ -23,7 +24,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 no parameter assigned function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +32,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 array is only 3 objects and starts at 0. no ob at pos 3 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..5c04a357 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,13 @@ function evenNumbers(n) { // TODO + const arr = []; + let i = 0; + while(i < n * 2) { + arr.push(i); + i +=2; + } + return arr; } 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..dfe20571 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,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..49af98c3 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 + let i = 0, j = 0, k =0 + if (n != 0) { + do { + i += 2 + k += i + j++ + } while(j < n-1) + return k + } else { + return 0 + } } 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..402be95f 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,10 @@ // 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. + \ 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..03da286f 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 1) { + total *= i; + } + } + return total; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..d8f4bb5b 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -9,9 +9,45 @@ 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. */ +// first get a list of all genres that are unique +// next would be to get the highest rated in that genre +//final would be to loop thru the book array and get the title with highest rating + +function uniqueGenres(books) { + let genres = []; + for (let book of books) { + genres.push (book.genre) + } + return [...new Set(genres)]; +} + +// highest rated title in each genres +function highestRated(books, genre) { + let ratingArray = []; + for( let book of books) { + if (book.genre === genre) { + ratingArray.push(book.rating); + } + } + return Math.max(...ratingArray); +} + + function getHighestRatedInEachGenre(books) { // TODO + let list = []; + let genres = uniqueGenres(books); + for (let book of books) { + for (let genre of genres) { + if ( + book.rating === highestRated(books, genre) && book.genre === genre + ) { + list.push(book.title); + } + } + } + return list; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..dbdfdee9 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,18 @@ function generateFibonacciSequence(n) { // TODO + let newSequence = []; + let amount =0; + for (let i = 0; i=2) { + amount = newSequence[i -1] + newSequence[i -2]; + newSequence.push(amount); + } else { + amount += i; + newSequence.push (amount); + } + } + return newSequence; } /* ======= TESTS - DO NOT MODIFY ===== */