diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..2c63c085 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..0c760ae5 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -19,9 +19,13 @@ function sayHello() { let message = "Hello"; } +// In the above example we wish to use the name that the user provides to greet them + let hello = sayHello(); console.log(hello); +// in the above example we need the users name to to log the function + // Example 3 function sayHelloToUser(user) { @@ -29,8 +33,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// the user needs to input username for the greeting Hello user to be printed // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// arr 3 is undefined as the array only currently goes to 2. Once the array another value is added to the array it will be printed in the terminal \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..cf7db406 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,16 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let arrayEven = []; + while (arrayEven.length < n) { + if (i % 2 === 0) { + arrayEven.push(i); + } + i++; + } +return console.log(arrayEven.toString()); + } 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..a1c05baa 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,15 @@ const BIRTHDAYS = [ "November 15th" ]; + function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < birthdays.length){ + if(birthdays[i].includes(`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..c4051007 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -5,9 +5,18 @@ 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; +let evenSum = [] function evenNumbersSum(n) { - // TODO + do{ + if(i % 2 === 0){ + evenSum.push(i); + total = total + i; + } + i ++; + } while (evenSum.length < n) + return total; } 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..40ce1cdb 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -7,7 +7,7 @@ // Change the below code to use a for loop instead of a while loop. let i = 0; -while(i < 26) { +for( i = 0; i < 26; i + 1 ) { console.log(String.fromCharCode(97 + i)); i++; } diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..6810abcc 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +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..f9bc7555 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (const value of tubeStations) { + console.log(value); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (const letter of str) { + console.log(letter.toUpperCase()) +}; diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..6c56b5e6 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,9 +12,17 @@ */ function getTemperatureReport(cities) { - // TODO + // i need to create a new array to push the items to + let reportArray =[]; + for (const element of cities) { + if(temperatureService(element)){ + reportArray.push(`The temperature in ${element} is ${temperatureService(element)} degrees`) + } + }return reportArray } + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..98e9e740 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 i; + do { + i = generateRandomNumber(); + } while (i <= 50); + return i; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..315f0fbd 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -4,8 +4,15 @@ 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 element of allArticleTitles) { + if (element.length <= 65); + articleArray.push(element) + } + }return articleArray } /* @@ -14,7 +21,8 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewest = (a, b) => a.length <= b.length ? a : b; + return allArticleTitles.reduce(fewest); } /* @@ -24,15 +32,28 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + { + let allArticleTitlesNew = [] + for (const element of allArticleTitles) { + if (element.includes(Number)){ + allArticleTitlesNew.push(element) + } +return allArticleTitlesNew +} } /* 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 avNum = 0 + for (const element of allArticleTitles) { + avNum += element.length; + } + let returnValue = avNum / allArticleTitles.length; + return Math.round(returnValue); +}*/ diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..ee2b0403 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averageArray = []; + let averagePrice; + + for (const array of closingPricesForAllStocks) { + let num = 0; + for (const number of array) { + num += number; + } + + averagePrice = num / 5; + averageArray.push(parseFloat(averagePrice.toFixed(2))); + } + return averageArray; + } /* @@ -49,6 +62,12 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let priceChanges = []; + for (const array of closingPricesForAllStocks) { + let comparePrices = (array.at(0) - array.at(-1)) * -1 ; + priceChanges.push(parseFloat(comparePrices.toFixed(2))) + } + return priceChanges; } /* @@ -65,6 +84,14 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let closingPrice = []; + for( i = 0; i < stocks.length; i++){ + let highestClosingPrice = Math.max(...closingPricesForAllStocks[i]); + let theHighPrice = highestClosingPrice.toFixed(2); + let theClosingPrice = `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${theHighPrice}`; + closingPrice.push(theClosingPrice); + } + return closingPrice; }