diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..14ad45b1 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,13 +12,13 @@ // Example 1 let a; console.log(a); - +// The variable is not assigned to any value. // Example 2 function sayHello() { let message = "Hello"; } - +//function doesn't return a value let hello = sayHello(); console.log(hello); @@ -29,8 +29,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// when function called to argument has given. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// there is only 0,1,2 index in this array only. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..1ed791cd 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,14 @@ function evenNumbers(n) { // TODO +let i = 0; +let st = ""; +while (i < n * 2 && i % 2 === 0){ + st+=i + + i+=2 +} +console.log(st) } evenNumbers(3); // should output 0,2,4 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..426e5b5b 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,11 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + while (i < birthdays.length) { + if (birthdays[i].startsWith("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..c82b499b 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,16 @@ function evenNumbersSum(n) { // TODO + let i = 0; + let sum = 0; + do { + if (i % 2 === 0) { + sum += i; + } + i++ + } + while (i < n * 2); +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..08aee244 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) { - console.log(String.fromCharCode(97 + i)); - i++; +for (let i = 0; i < 26 ; i++) { +console.log(String.fromCharCode(97 + i)); } +// let i = 0; +// while(i < 26) { +// 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..afe6af32 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++) { + 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..a7dcb865 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (const element of tubeStations) { + console.log(element); + } // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (const 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..f1fe3154 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,11 @@ function getTemperatureReport(cities) { // TODO + let report = []; + for (let i = 0; i < cities.length; i++) { + report[i] = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`; + } + return report; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..4338f365 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,12 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let counter = 0; + do { + counter = generateRandomNumber(); + } while ( counter<=50); + return counter; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..024bb3ad 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,7 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + return allArticleTitles.filter(title => title.length <= 65); } /* @@ -14,7 +14,14 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let shortestTitle = allArticleTitles[0]; + + allArticleTitles.forEach(title => { + if (title.length < shortestTitle.length) { + shortestTitle = title + } + }) + return shortestTitle } /* @@ -23,7 +30,10 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + return allArticleTitles.filter(title => { + const numbersInTitle = title.match(/[1-9]/g) + return numbersInTitle != null ? true : false + }) } /* @@ -31,7 +41,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + const articleTitleLengths = allArticleTitles.map(title => title.length) + + const sumOfTitleLengths = articleTitleLengths.reduce(function(partialSum, a) {return partialSum + a}, 0) + + return Math.round(sumOfTitleLengths / allArticleTitles.length) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..763129c0 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,11 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + return closingPricesForAllStocks.map(stockPrices => { + const sumOfStockPrices = stockPrices.reduce(function(partialSum, a) {return partialSum + a}, 0) + + return Number((sumOfStockPrices / stockPrices.length).toFixed(2)); + }) } /* @@ -48,7 +52,9 @@ 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 + return closingPricesForAllStocks.map(prices => { + return Number((prices[prices.length -1] - prices[0]).toFixed(2)); + }) } /* @@ -64,7 +70,14 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const highestPrices = [] + + for (let i = 0; i < closingPricesForAllStocks.length; i++){ + const maxPrice = Math.max(...closingPricesForAllStocks[i]) + highestPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${maxPrice.toFixed(2)}`) + } + + return highestPrices } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..0daab393 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,9 +10,14 @@ function factorial(input) { // TODO -} + let factorial = input; -/* ======= TESTS - DO NOT MODIFY ===== */ + for (let i = input-1; i > 1; i--) { + factorial *= i; + } + return factorial; +} +// /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => { expect(factorial(3)).toEqual(6);