diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..a3d06ede 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,12 +10,12 @@ */ // Example 1 -let a; +let a; // there is no value has been added to a therefore a is undefined. console.log(a); // Example 2 -function sayHello() { +function sayHello() { // there is no parameter passed to the function's argument. let message = "Hello"; } @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); // again there is no value been passed when calling the function. it should look like sayHelloToUser(value); // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1,2,3]; +console.log(arr[3]); // there are only 2 elements in this array (starting from 0), so the output will be undefined. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8e810d3a 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -5,10 +5,18 @@ The list of numbers should start with 0. n is being passed in as a parameter. */ +const evenArray = []; + function evenNumbers(n) { - // TODO + let i = 0; + while (evenArray.length < n) { + evenArray.push(i); + i += 2; + } + + console.log(evenArray.toString()); } -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(3); // should output 0,2,4 +// evenNumbers(0); // should output nothing +evenNumbers(20); // 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..e7d5661d 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,10 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO +// let i = 0; +// while (i < BIRTHDAYS.length){ + return BIRTHDAYS.find(BIRTHDAYS => BIRTHDAYS === "July 11th"); +// } // TODO } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log(findFirstJulyBDay(BIRTHDAYS.toString())); // should output "July 11th" diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..6c613fbc 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -5,11 +5,24 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ - +let i = 0; +let total = 0; +const evenArray = []; +// let sum = 0; function evenNumbersSum(n) { - // TODO + + do { + if (i%2 === 0){ + evenArray.push(i); + total = total + i; + } + i++; + + } while (evenArray.length < n); + + return total; } -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(3)); // should output 6 +console.log(evenNumbersSum(10)); // should output 0 +// 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..61227311 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,7 @@ // 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..919ae992 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -11,23 +11,20 @@ */ const WRITERS = [ - "Virginia Woolf", - "Zadie Smith", - "Jane Austen", - "Bell Hooks", - "Yukiko Motoya" -] - -const AGES = [ - 59, - 40, - 41, - 63, - 49 + "Virginia Woolf", + "Zadie Smith", + "Jane Austen", + "Bell Hooks", + "Yukiko Motoya", ]; +const AGES = [59, 40, 41, 63, 49]; + // TODO - Write for loop code here +for (let i = 0; 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..0154ec0f 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,16 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (const eachStation of tubeStations){ + console.log(eachStation); +} + + + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (element of str){ + console.log(element.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..00d9160d 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + let tempReport = []; + for (let city of cities){ + let temp = temperatureService(city); + tempReport.push(`The temperature in ${city} is ${temp} degrees`); + } + return tempReport; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..7162ada0 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,13 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let generateNum; + + do{ + generateNum = generateRandomNumber(); + } + while(generateNum <= 50); + return generateNum; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..534f4c2d 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,12 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let articleArr = []; + for (const element of allArticleTitles) { + if (element.length <= 65){ + articleArr.push(element); + } + return articleArr; } /* @@ -14,7 +19,8 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestWords = (a, b) => a.length <= b.length ? a : b; + return allArticleTitles.reduce(fewestWords); } /* @@ -22,16 +28,40 @@ function titleWithFewestWords(allArticleTitles) { 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 checkTitleContainNum(title){ + for(let character of title){ + if(character >= '0' && character <= '9'){ + return true; + } + else { + return false; + } + } +} function headlinesWithNumbers(allArticleTitles) { - // TODO + let articleWNum = []; + + for (let title of allArticleTitles){ + if(checkTitleContainNum(title)){ + articleWNum.push(title); + } + } + return articleWNum; } + /* 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 totalChar = 0; + + for(let title of allArticleTitles){ + totalChar += title.length; + } + return Math.round(totalChar / allArticleTitles.length); } @@ -79,3 +109,4 @@ test("should only return headlines containing numbers", () => { test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); }); + diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..4e42c9af 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averages = []; + + for (let pricesForStock of closingPricesForAllStocks) { + averages.push(getAveragePricesForStock(pricesForStock)); + } + + return averages; +} + +function getAveragePricesForStock(pricesForStock) { + let total = 0; + + for (let price of pricesForStock) { + total += price; + } + + return roundTo2Decimals(total / pricesForStock.length); +} + +function roundTo2Decimals(num) { + return Math.round(num * 100) / 100; } /* @@ -48,7 +68,19 @@ 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 changes = []; + + for (let pricesForStock of closingPricesForAllStocks) { + changes.push(getPriceChangeForStock(pricesForStock)); + } + + return changes; +} + +function getPriceChangeForStock(pricesForStock) { + let priceChange = + pricesForStock[pricesForStock.length - 1] - pricesForStock[0]; + return roundTo2Decimals(priceChange); } /* @@ -64,9 +96,41 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = getHighestPrice(closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; } +function getHighestPrice(pricesForStock) { + + let highestPriceSoFar = 0; + + for(let price of pricesForStock) { + + if(price > highestPriceSoFar) { + highestPriceSoFar = price; + } + } + + return highestPriceSoFar; +} + +function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) { + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = Math.max(...closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; + + /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => {