diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..b8f7dfbe 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,6 +12,8 @@ // Example 1 let a; console.log(a); +// it doesn't have value. + // Example 2 @@ -21,6 +23,8 @@ function sayHello() { let hello = sayHello(); console.log(hello); +//it doesn't return value + // Example 3 @@ -28,9 +32,12 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } + sayHelloToUser(); +//it hasn't value for user parameter // Example 4 let arr = [1,2,3]; console.log(arr[3]); + //array hasn't this value diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..280866af 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(10)); + 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..8648b8c8 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -15,9 +15,13 @@ const BIRTHDAYS = [ "September 28th", "November 15th" ]; - 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" +console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" \ No newline at end of file diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..b090df10 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -6,9 +6,16 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ -function evenNumbersSum(n) { - // TODO -} + function evenNumbersSum(n) { + let sum = 0; + let i = 0; + do { + sum += i * 2; + i++; + } while (i < n); + + return sum; + } console.log(evenNumbersSum(3)); // should output 6 console.log(evenNumbersSum(0)); // should output 0 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..c983d000 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++; +// } + +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..26096fd9 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,10 @@ 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..529e09f7 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -12,5 +12,14 @@ let tubeStations = [ ]; -// TODO Use a for-of loop to capitalise and output each letter in the string seperately. +for(let station of tubeStations){ + console.log(station); +} + +// TODO Use a for-of loop to capitalise and output each letter in the string separately. let str = "codeyourfuture"; + + +for (let letters of str){ + console.log(letters.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..ddbb3744 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,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..d069c4cf 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,14 @@ 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..af41ef65 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 + if(allArticleTitles.lengh !== 0){ + allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <= 65); + return allArticleTitles; + +} +else return allArticleTitles=[]; } /* @@ -13,8 +18,12 @@ function potentialHeadlines(allArticleTitles) { Implement the function below, which returns the title with the fewest words. (you can assume words will always be seperated by a space) */ + function titleWithFewestWords(allArticleTitles) { - // TODO + let arr = allArticleTitles; + let newArr = arr.sort((a, b) => a.length - b.length); + return newArr[0]; + } /* @@ -23,7 +32,22 @@ 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 arr = []; + + for (let moreClick of allArticleTitles) { + let choosen = ""; + for (let letter of moreClick) { + if (!isNaN(letter) && letter !== " ") { + choosen = moreClick; + break; + } + } + if (choosen.length !== 0) { + arr.push(choosen); + } + } + return arr; + } /* @@ -31,11 +55,17 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let total = 0; + let average = 0; + for (let articleTitle of allArticleTitles) { + total += articleTitle.length; + } + average = total / allArticleTitles.length; + return Math.round(average); + // TODO } - /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ "Streaming wars drive media groups to spend more than $100bn on new content", diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..31f3370b 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,7 +63,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 + // TODO + let newPriceChange = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + newPriceChange.push((closingPricesForAllStocks[i][4] - closingPricesForAllStocks[i][0]).toFixed(2) * 1); + } + return newPriceChange; } /* @@ -65,9 +85,19 @@ 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; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(