diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..247d9967 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,7 +12,7 @@ // Example 1 let a; console.log(a); - +//Because we didn't assign a value for a variable a when we first declaring it. // Example 2 function sayHello() { @@ -21,16 +21,19 @@ function sayHello() { let hello = sayHello(); console.log(hello); +//Because the function is not returning anything. // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); } sayHelloToUser(); +//we didn't pass argument to the function when we are calling it. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//Because we don't have index 3. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8c33a350 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,19 @@ */ function evenNumbers(n) { - // TODO + // TODO + let arr = []; + let i = 0; + while (i % 2 === 0 && n > 0 && n > arr.length) { + arr.push(i); + i += 2; + + } + + return arr.toString(); // changes the numbers to strings } -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 + +console.log(evenNumbers(3)); // should output 0,2,4 +console.log(evenNumbers(0)); // should output nothing +console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..9ddea110 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,15 @@ 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..70752a6e 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -6,10 +6,24 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ -function evenNumbersSum(n) { +function evenNumbersSum(num) { // TODO + let i = 0; + let sum = 0; + let number = 0; + + do{ + sum = sum + number; + number = number + 2; + i = i + 1; + + + } + while(i < num); + 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..cd7839ad 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -12,3 +12,8 @@ while(i < 26) { 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..3e487efc 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,10 @@ const AGES = [ ]; // TODO - Write for loop code here +for(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..4e8a4a07 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; - +for(tubes of tubeStations) { + console.log(tubes); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +let result = str.toUpperCase(); +for(letter of result) { + console.log(letter); +} + diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..deb1522f 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,16 @@ function getTemperatureReport(cities) { // TODO + arrayOfStrings = []; + for(let i = 0; i < cities.length; i++) { + arrayOfStrings.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees"); + + } + return arrayOfStrings; } + /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { @@ -32,6 +39,7 @@ function temperatureService(city) { return temparatureMap.get(city); } + test("should return a temperature report for the user's cities", () => { let usersCities = [ "London", diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..7617d02d 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -7,10 +7,17 @@ function generateRandomNumber() { console.log("Generating number..."); return Math.round(Math.random() * 100); + } function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let number; + do{number = generateRandomNumber() + + } + while(number <= 50) + return number; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..ca15bf27 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,8 +5,17 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + // TODO + let newArrayTitle = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + newArrayTitle.push(allArticleTitles[i]); + } + } + return newArrayTitle; } + + /* The editor of the FT likes short headlines with only a few words! @@ -14,16 +23,42 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO -} + let newString = '' + for (let i = 0; i < allArticleTitles.length; i++) { + if (newString.length < 1 || newString.split(" ").length > allArticleTitles[i].split(" ").length) { + newString = allArticleTitles[i]; + } + + } +return newString; +} /* The editor of the FT has realised that headlines which have numbers in them get more clicks! Implement the function below to return a new array containing all the headlines which contain a number. (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let newClicks = []; + + for (let i = 0; i < allArticleTitles.length; i++) { + for (let letter of allArticleTitles[i]) { + if (letter === "0"|| + letter === "1"|| + letter === "2"|| + letter === "3"|| + letter === "4"|| + letter === "5"|| + letter === "6"|| + letter === "7"|| + letter === "8"|| + letter === "9") { + newClicks.push(allArticleTitles[i]) + } + + } + } + return newClicks; } /* @@ -31,7 +66,12 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let totalLength = 0; + + for (let i = 0; i < allArticleTitles.length; i++) { + totalLength += allArticleTitles[i].length + } + return (Math.round(totalLength / allArticleTitles.length)) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..8d75e402 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let newAverage =[] + let sum = 0; + for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) { + for (let j = 0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++) { + sum += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i][j]; + } + newAverage.push( + (sum / CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length).toFixed(2) * + 1 + ); + + sum = 0; + } + return newAverage; + } /* @@ -48,9 +63,13 @@ function getAveragePrices(closingPricesForAllStocks) { The price change value should be rounded to 2 decimal places, and should be a number (not a string) */ function getPriceChanges(closingPricesForAllStocks) { - // TODO + // TODO + let newPriceChange = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + newPriceChange.push((closingPricesForAllStocks[i][4] - closingPricesForAllStocks[i][0]).toFixed(2) * 1); + } + return newPriceChange; } - /* 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 @@ -65,6 +84,15 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + highestPrice =[]; + for(let i=0; i < closingPricesForAllStocks.length; i++) { + let highestNum = closingPricesForAllStocks[i].sort(function(a,b){ + return b-a;} )[0]; + highestPrice.push( + "The highest price of " + stocks[i].toUpperCase() + " " + "in the last 5 days was " + highestNum.toFixed(2)); + + } + return highestPrice; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..c067b61c 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,12 @@ function factorial(input) { // TODO + + for (let i = input - 1; i >= 1; i--) { + input= input*i; + console.log(input) + } + return input; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..69e86856 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,14 @@ function getHighestRatedInEachGenre(books) { // TODO + let bookTitles=[]; + for(let i=0; i 4.8){ + bookTitles.push(books[i].title) + } + + } +return bookTitles; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..790fa662 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,8 +15,18 @@ function generateFibonacciSequence(n) { // TODO + + let fib = [0, 1]; + for(i = 2; i < n; i++) { + let y = fib[i-2] + fib[i-1]; + fib.push(y); + } + + + return fib; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => { expect(generateFibonacciSequence(10)).toEqual(