diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..b9c8ec9f 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,17 +10,18 @@ */ // Example 1 +// We didn't give a value to a variable a let a; console.log(a); // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; // we have to return something } let hello = sayHello(); -console.log(hello); +console.log(hello); // we need quotation marks // Example 3 @@ -28,9 +29,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); // there is not argument // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); // there are only 3 elements in the array. The last index should be 2 diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8ad2b1af 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,18 @@ */ function evenNumbers(n) { - // TODO + if (n === 0) { + console.log("nothing") + } else { + let i = 0; + let newString = ""; + while (i < n) { + newString = newString + 2*i + ","; + i++; + } + console.log(newString.substring(0, newString.length-1)); + } } - 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..73cd1b97 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,12 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + + let i = 0; + while (birthdays[i].substring(0, 4) !== "July") { + i++; + } + 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..f05ea323 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 i = 0; + let sum = 0; + do { + sum = sum + 2*i; + 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..800c2b66 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,15 @@ // 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++; } + + +for (let i = 0; i < 26; i++) { + console.log(String.fromCharCode(97 + 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..e5a08e81 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,13 @@ 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..797e0a0b 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,15 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let station of tubeStations) { + console.log(station); +} + + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (let letter of str) { + console.log(letter.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..611e7fd8 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let answer = []; + for (let i = 0; i < cities.length; i ++) { + answer.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees") + } + return answer; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..ed2fd6ed 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,11 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let randomNum = 0; + do { + randomNum = generateRandomNumber(); + } while (randomNum <=50); + return randomNum; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..936ede58 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 + let newArticle = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + newArticle.push(allArticleTitles[i]) + } + } + return newArticle; } /* @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let minWord = allArticleTitles[0].split(" ").length; + let j = 0; + for (let i = 0; i < allArticleTitles.length; i++ ) { + if (allArticleTitles[i].split(" ").length < minWord) { + minWord = allArticleTitles[i].split(" ").length; + j = i; + } + + } + return allArticleTitles[j]; + } /* @@ -23,7 +39,15 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let arrNum = []; + for (let i = 0; i < allArticleTitles.length; i++) { + for (let j = 0; j <= 9; j++) { + if (allArticleTitles[i].includes(j) && !arrNum.includes(allArticleTitles[i])) { + arrNum.push(allArticleTitles[i]); + } + } + } + return arrNum; } /* @@ -31,7 +55,11 @@ function headlinesWithNumbers(allArticleTitles) { 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 < allArticleTitles.length; i++) { + sum = sum + allArticleTitles[i].length; + } + return Math.round(sum/allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..5c3dd7b9 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,8 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO -} + let averageArr = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let sumAverage = 0; + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + sumAverage = sumAverage + closingPricesForAllStocks[i][j]; + } + averageArr.push(Math.round(sumAverage/closingPricesForAllStocks[i].length * 100) / 100); + } + return averageArr; + } /* We also want to see what the change in price is from the first day to the last day for each stock. @@ -48,7 +56,11 @@ 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 + let priceChange = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + priceChange.push(Math.round((closingPricesForAllStocks[i][closingPricesForAllStocks[i].length-1] - closingPricesForAllStocks[i][0]) * 100) / 100); + } + return priceChange; } /* @@ -64,7 +76,18 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let maxPrice = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = closingPricesForAllStocks[i][0]; + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + if (closingPricesForAllStocks[i][j] > highestPrice) { + highestPrice = closingPricesForAllStocks[i][j]; + } + } + let str = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestPrice.toFixed(2); + maxPrice.push(str); + } + return maxPrice; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..d3d939da 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,11 @@ */ function factorial(input) { - // TODO + let production = 1; + for (let i = 1; i <=input; i++ ) { + production = production * i; + } + return production; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..224603f1 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,7 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..30c1ffc7 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,12 @@ */ function generateFibonacciSequence(n) { - // TODO + let fibArr = [0, 1]; + for (let i = 0; i < n-2; i++ ) { + fibArr.push(fibArr[fibArr.length-1] + fibArr[fibArr.length-2]); + + } + return fibArr; } /* ======= TESTS - DO NOT MODIFY ===== */