From a7be5866e721c9fcec6f6ad3c269c19d57f16f74 Mon Sep 17 00:00:00 2001 From: kavitappatil Date: Tue, 6 Sep 2022 19:44:03 +0100 Subject: [PATCH 1/2] exercise --- 1-exercises/A-undefined/exercise.js | 10 +++++----- 1-exercises/B-while-loop/exercise.js | 12 +++++++++++- 1-exercises/C-while-loop-with-array/exercise.js | 13 ++++++++++++- 1-exercises/D-do-while/exercise.js | 15 ++++++++++++++- 1-exercises/E-for-loop/exercise1.js | 12 +++++++++--- 1-exercises/E-for-loop/exercise2.js | 7 ++++++- 1-exercises/F-for-of-loop/exercise.js | 7 +++++++ 7 files changed, 64 insertions(+), 12 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..df5b188e 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,16 +12,15 @@ // Example 1 let a; console.log(a); - +//a has not been assigned any value. // Example 2 function sayHello() { let message = "Hello"; -} - +} let hello = sayHello(); console.log(hello); - +// function has not return value. // Example 3 function sayHelloToUser(user) { @@ -29,8 +28,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// function does not pass the parameter value. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// index 3 does not exist. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..75736373 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,17 @@ */ function evenNumbers(n) { - // TODO + + let i = 0; + + while (n > 0) { + + if(i % 2 == 0){ + console.log(i); + n = n - 1; + } + i = i + 1; + } } 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..0725790f 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,18 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + let arrayLength = birthdays.length; + while(i < arrayLength) { + + if (birthdays[i].startsWith("July")) { + console.log(birthdays[i]); + break; + } + + 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..86fe411d 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,20 @@ */ function evenNumbersSum(n) { - // TODO + let i = 0; + let sumofnumbers = 0; + do { + if(i % 2 == 0) { + sumofnumbers = sumofnumbers + i; + + n = n - 1; + } + + i = i + 1; + + + } while (n > 0) + console.log(sumofnumbers); } 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..27412c6f 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) { +// let i = 0; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } + + +for (i = 0 ;i < 26 ; i++) { 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..5c918c9e 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,7 +26,12 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here +const combined=[] + +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..e00a6775 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let x of tubeStations) { + console.log(text); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (const value of str) { + console.log(value.toUpperCase()); +} From 97e427d421e57036333d0c1cb3158b31e67911eb Mon Sep 17 00:00:00 2001 From: kavitappatil Date: Tue, 6 Sep 2022 23:33:14 +0100 Subject: [PATCH 2/2] mandatory --- 2-mandatory/1-weather-report.js | 7 ++- 2-mandatory/2-retrying-random-numbers.js | 6 ++- 2-mandatory/3-financial-times.js | 56 +++++++++++++++++++++--- 2-mandatory/4-stocks.js | 43 ++++++++++++++++-- 4 files changed, 102 insertions(+), 10 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..d3402290 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + let tempArray = []; + for (i=0; i < cities.length; i++ ) { + let deg = temperatureService(cities[i]); + tempArray.push(`The temperature in ${cities[i]} is ${deg} degrees`); + } + return tempArray; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..b4e1cab6 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 randomNumber = 0; + do { + randomNumber = generateRandomNumber(); + } while ( randomNumber <= 50 ); + return randomNumber; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..cbba1e7e 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -4,8 +4,16 @@ The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. Implement the function below, which will return a new array containing only article titles which will fit. */ -function potentialHeadlines(allArticleTitles) { - // TODO +function potentialHeadlines(allArticleTitles) +{ + let articleArray = []; + { + for (const currentArticle of allArticleTitles) { + if (currentArticle.length <= 65) { + articleArray.push(currentArticle); + } + } + }return articleArray } /* @@ -14,7 +22,25 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let articleWithFewestWords = 0; + let numberOfWords = 0; + + for (const currentArticle of allArticleTitles) { + + numberOfWordsCurrent = currentArticle.length; + + if ( numberOfWords == 0 ) { + numberOfWords = numberOfWordsCurrent; + articleWithFewestWords = currentArticle; + } + + if (numberOfWordsCurrent < numberOfWords) { + numberOfWords = numberOfWordsCurrent; + articleWithFewestWords = currentArticle; + } + } + + return articleWithFewestWords; } /* @@ -23,7 +49,20 @@ 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 headlineWithNumbers = []; + let digits = /\d+/g; + let headlines = potentialHeadlines(allArticleTitles); + + for (const headline in headlines) { + + if (headline.match(digits)) { + headlineWithNumbers.push(headline); + } + } + + return headlineWithNumbers; + } /* @@ -31,7 +70,14 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let lengthOfAllArticles = 0; + + for (let i = 0; i < allArticleTitles.length; i++) { + lengthOfAllArticles += allArticleTitles[i].length; + } + + let averageChar = lengthOfAllArticles / allArticleTitles.length; + return Math.round(averageChar); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..48f5d544 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averagePriceArr = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let totalStock = 0; + let averageStockSum = 0; + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + totalStock += closingPricesForAllStocks[i][j]; + } + averageStockSum = totalStock / closingPricesForAllStocks[i].length; + let value = averageStockSum.toFixed(2); + averagePriceArr.push(parseFloat(value)); + } + + return averagePriceArr; } /* @@ -48,7 +60,18 @@ 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 priceChangeValue = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let difference = closingPricesForAllStocks[i].length - 1; + let totalDifference = + closingPricesForAllStocks[i][difference] - + closingPricesForAllStocks[i][0]; + + priceChangeValue.push(parseFloat(totalDifference.toFixed(2))); + } + + return priceChangeValue; } /* @@ -64,7 +87,21 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPrices = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let total = 0; + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + if (closingPricesForAllStocks[i][j] > total) { + total = closingPricesForAllStocks[i][j]; + } + } + highestPrices.push( + `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${total.toFixed(2)}` + ); + } + + return highestPrices; }