diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..de84a0d7 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,16 +11,16 @@ // Example 1 let a; -console.log(a); +console.log(a); //a has not been defined, just declared // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; //nothing is being returned } let hello = sayHello(); -console.log(hello); +console.log(hello); //it calls a function which doesn't return anything // Example 3 @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //no parameter entered // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1, 2, 3]; +console.log(arr[3]); //indexes start at 0, there is no 3rd index set diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..aa11d699 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,16 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let evens = []; + while (n > evens.length) { + evens[0] = 0; + i += 2; + evens.push(i); + } + console.log(`${evens}`); } 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 +evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 \ No newline at end of file diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..bc6f67f1 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) { + i++ + if (birthdays[i] === 'July 11th') { + return birthdays[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..153731dd 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,15 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + let i = 0; + do { + if (i % 2 === 0) { + sum += i * 3; + } + 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..9a0f0b84 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) { +// 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..ba35a9c0 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 < AGES.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..6fc78096 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,16 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +let stations = ''; +for (tubes of tubeStations) { + stations += tubes + ', '; +} +console.log(stations); // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +let newStr = ''; +for (letter of str) { + newStr += letter.toUpperCase(); +} +console.log(newStr); diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..8b09c355 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -10,16 +10,21 @@ For example, "The temperature in London is 10 degrees" - Hint: you can call the temperatureService function from your function */ - +ct = ['London', 'Paris', 'Barcelona', 'Dubai', 'Mumbai', 'São Paulo', 'Lagos']; function getTemperatureReport(cities) { - // TODO + + let statement = []; + for (let i = 0; i < cities.length; i++) { + let sentance = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees` + statement.push(sentance) + } + return statement; } - - +console.log(getTemperatureReport(ct)); /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); + let temparatureMap = new Map(); temparatureMap.set('London', 10); temparatureMap.set('Paris', 12); @@ -28,7 +33,7 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..86c772bd 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 + do { + randomNum = generateRandomNumber(); + } while (randomNum < 50); + + return randomNum; } +console.log(getRandomNumberGreaterThan50()); /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..fe545517 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + lessThan65 = [] + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + lessThan65.push(allArticleTitles[i]); + } + } + return lessThan65; } /* @@ -14,7 +20,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 50) { + return allArticleTitles[i]; + } + } } /* @@ -23,15 +33,27 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + withNums = [] + for (let i = 0; i < allArticleTitles.length; i++) { + if (/\d/.test(allArticleTitles[i])) { + withNums.push(allArticleTitles[i]); + } + } + return withNums; } + /* 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; + let numOfArticles = allArticleTitles.length; + for (let i = 0; i < allArticleTitles.length; i++) { + sum += allArticleTitles[i].length; + } + return Math.round(sum / numOfArticles); } @@ -49,7 +71,10 @@ const ARTICLE_TITLES = [ "The three questions that dominate investment", "Brussels urges Chile's incoming president to endorse EU trade deal", ]; - +// console.log(potentialHeadlines(ARTICLE_TITLES)) +// console.log(titleWithFewestWords(ARTICLE_TITLES)) +// console.log(headlinesWithNumbers(ARTICLE_TITLES)) +//console.log(averageNumberOfCharacters(ARTICLE_TITLES)) /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..e36bf8e7 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,10 +33,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ -function getAveragePrices(closingPricesForAllStocks) { - // TODO + +function getSum(arr) { + let sum = 0; + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + } + return sum; } +function getAveragePrices(closingPricesForAllStocks) { + let sum = 0; + let average = 0 + let result = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + sum = getSum(closingPricesForAllStocks[i]) + average = sum / 5; + result.push(Math.round(average * 100) / 100); + } + return result; +} +getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS); /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which @@ -47,10 +64,18 @@ function getAveragePrices(closingPricesForAllStocks) { (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 The price change value should be rounded to 2 decimal places, and should be a number (not a string) */ -function getPriceChanges(closingPricesForAllStocks) { - // TODO +function findDifference(arr) { + return arr[4] - arr[0]; } +function getPriceChanges(closingPricesForAllStocks) { + let result = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + result.push(Number(findDifference(closingPricesForAllStocks[i]).toFixed(2))); + } + return result; +} +getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. Implement the below function, which @@ -64,9 +89,13 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let result = [] + for (let i = 0; i < closingPricesForAllStocks.length && i < stocks.length; i++) { + result.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`) + } + return result; } - +highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..82e25dfa 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,8 +9,17 @@ */ function factorial(input) { - // TODO + let result = 1; + if (input === 0 || input === 1) { + return input; + } else { + for (let i = input; i >= 1; i--) { + result = result * i; + } + } + return result; } +console.log(factorial(3)) /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..b393e7a4 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -9,9 +9,14 @@ 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. */ +//get books by genre +//search highest rated using ge +//return highest rated +function getGenre(books){ +} function getHighestRatedInEachGenre(books) { - // TODO + } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..0636c69d 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,8 +14,17 @@ */ function generateFibonacciSequence(n) { - // TODO -} + if (n === 1) { + return [0, 1]; + } + else { + let result = generateFibonacciSequence(n - 1); + result.push(result[result.length - 1] + result[result.length - 2]); + return result; + } +}; +console.log(generateFibonacciSequence(10)); +//I used google /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => {