diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..36e65c72 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,7 +11,8 @@ // Example 1 let a; -console.log(a); +console.log(a); +// We declare the variable 'a' but we haven't assigned a value to it yet. // Example 2 @@ -22,6 +23,8 @@ function sayHello() { let hello = sayHello(); console.log(hello); +// We are not returning anything from the function body and so the default return is undefined. + // Example 3 function sayHelloToUser(user) { @@ -30,7 +33,11 @@ function sayHelloToUser(user) { sayHelloToUser(); +// We are not providing any arguments for the function call and so the user is undefined. + // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); + +// arr[3] is the 4th item of the array which has not been assigned a value and so it's undefined. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..bb6158cb 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,17 @@ */ function evenNumbers(n) { - // TODO + let arr = []; + let i = 0; + while (i%2 === 0 && n > 0 && n > arr.length) { + arr.push(i); + i+=2; + } + return arr.toString(); } +console.log(evenNumbers(3)); + 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 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..1e608421 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ 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..72e28204 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,16 @@ */ function evenNumbersSum(n) { - // TODO + let i = 0; + let sumTotal = 0; + do { + i++; + if (i % 2 == 0) { + sumTotal += i * 3; + } + } while (i < n); + + return sumTotal; } 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..2e27d244 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,13 @@ // 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..f0fc2903 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,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..de417e35 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let trainStops of tubeStations) { + console.log(trainStops); +} -// TODO Use a for-of loop to capitalise and output each letter in the string seperately. + +// TODO Use a for-of loop to capitalize and output each letter in the string separately. let str = "codeyourfuture"; + +for (let letterCapitalize of str) { + console.log(letterCapitalize.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..e24d4398 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -2,7 +2,7 @@ Imagine we're making a weather app! We have a list of cities that the user wants to track. - We also already have a temperatureService function which will take a city as a parameter and return a temparature. + We also already have a temperatureService function which will take a city as a parameter and return a temperature. Implement the function below: - take the array of cities as a parameter @@ -12,10 +12,14 @@ */ function getTemperatureReport(cities) { - // TODO + let newArray = []; + for (let i = 0; i < cities.length; i++){ + newArray.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees"); + + } + return newArray; } - /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..ad221691 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,9 +10,15 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let num; + do { + num = generateRandomNumber(); + + } while (num <= 50) + return num; } + /* ======= 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..c4efb2d3 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,25 +5,62 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let newArray = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + newArray.push(allArticleTitles[i]); + } + } + return newArray; } /* 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) + (you can assume words will always be separated 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! + The editor of the FT has realized 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 +68,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..d17892d0 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,8 +35,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let newAverage =[] + let sum = 0; + for(let i = 0; i < 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; } + /* 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 @@ -48,7 +60,12 @@ 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 + getPriceChange = []; + for (price of closingPricesForAllStocks) { + let priceChange = Number((price[price.length -1] - price[0]).toFixed(2)); + getPriceChange.push(priceChange); + } + return getPriceChange; } /* @@ -60,11 +77,15 @@ function getPriceChanges(closingPricesForAllStocks) { - Returns an array of strings describing what the highest price was for each stock. For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" The test will check for this exact string. - The stock ticker should be capitalised. + The stock ticker should be capitalized. The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + highestPriceLast5Days = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`); + } + return highestPriceLast5Days; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..5571edd9 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,9 +9,16 @@ */ function factorial(input) { - // TODO + if (input === 0 || input === 1) + return 1; + for(let i = input -1; i >= 1 ; i--) { + input *= i; + } + return input; } +console.log(factorial(3)) + /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => { diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..5786eef5 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,15 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + + let newArray = []; + + for (let i = 0; i < books.length; i++) { + if(books[i].rating > 4.8) { + newArray.push(books[i].title) + } + } + return newArray; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..807f4570 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,14 @@ */ function generateFibonacciSequence(n) { - // TODO + let fib = [0, 1]; + let newFib = [0, 1]; + + for(let i = 2; i < n; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + newFib.push(fib[i]); + } + return newFib; } /* ======= TESTS - DO NOT MODIFY ===== */