diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..8cf7f802 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,12 +11,12 @@ // Example 1 let a; -console.log(a); +console.log(a); // Variable a is declared but no value is assigned to it // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; // there is no return statemnet in the function sayHello(), the variable message only exists inside of the function,but nothing is returned } let hello = sayHello(); @@ -28,9 +28,8 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); - +sayHelloToUser(); // the parameter is not defined in the parenthesis so it returns undefined // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); // the index 3 in the array call points to the 4th placed element inside the array, as we count the elements from index 0, index 3 is undefined \ 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..0628035c 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,19 @@ */ function evenNumbers(n) { - // TODO + // if (!n) return + let i = 0; + let arr = []; + while (i < n * 2) { + if (i % 2 === 0) { + arr.push(i); + } + i++; + } + console.log(arr); } 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(1); \ No newline at end of file diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..298de79d 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,16 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + let birthday; + while (i < birthdays.length) { + if (birthdays[i].includes("July")) { + birthday = birthdays[i]; + i++ + } + i++; + } + return birthday } 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..d09e517b 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,14 @@ */ function evenNumbersSum(n) { - // TODO + let result = 0; + let i = 0; + do { if (i % 2 === 0) + result += i; + i++; + } + while (i < n * 2) + return result; } 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..80ce1011 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,14 @@ // 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 (let 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..fd2c08d1 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,6 +26,10 @@ const AGES = [ 49 ]; +for (let i = 0; i < 5; i++) { + console.log(`${WRITERS[i]} is ${AGES[i]} years old`) +} + // TODO - Write for loop code here /* diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..e1e234c5 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 (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()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..cd71907b 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,10 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + let arr = []; + for (const element of cities) { + if (temperatureService(element)) { + arr.push(`The temperature in ${element} is ${temperatureService(element)} degrees`); + } + } return arr; } - - /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..7a62d716 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -9,8 +9,22 @@ function generateRandomNumber() { return Math.round(Math.random() * 100); } +// function getRandomNumberGreaterThan50() { +// return generateRandomNumber() + 50; +// } + function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let i = 0; + let arr = []; + do { + arr.push(generateRandomNumber()); + if (arr[i] > 50) { + arr = arr[i]; + } + i++; + } + while (i <= arr.length); + return arr } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..73877e6d 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let arr = []; + for (const element of allArticleTitles) { + if (element.length <= 65) { + arr.push(element); + } + }return arr; + } /* @@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let arrLength = []; + let smallestNumValue; + let index; + for (const element of allArticleTitles) { //my first step was to itirate through the array and create an other array with indexes of lenght of each string + arrLength.push(element.split(" ").length); + } + smallestNumValue = Math.min(...arrLength); //once i had the array of indexes of lenght I went on to find the position of the smallest index in the array in 2 steps, first finding the value of the smallest number + index = arrLength.indexOf(smallestNumValue)//this is the second step of finding the position of the smallest index in an array, this steps finds the position of the smallest number that we identified in the previous step + return allArticleTitles[index]; //finally i return the position of the smallest number of the original array } /* @@ -23,7 +37,12 @@ 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 array = [] + for (const element of allArticleTitles){ + if ( /\d/.test(element)) { + array.push(element); + } + } return array; } /* @@ -31,11 +50,22 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let arrNumOfCharacters = []; + let total = 0; + let average = 0; + for (const element of allArticleTitles) { + arrNumOfCharacters.push(element.trim().length); + } + for (const element of arrNumOfCharacters) { + total = total + element; + } + average = total / arrNumOfCharacters.length; + return Math.round(average); } + /* ======= 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..1579b3d5 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let total = 0; + let average = 0; + arrayOfAverages = []; + for (const subArray of closingPricesForAllStocks) { + for (const element of subArray) { + total = total + element; + } + average = total / subArray.length; + total = 0; + arrayOfAverages.push(Math.round(average *100)/100); + } + return arrayOfAverages; } /* @@ -48,7 +59,15 @@ 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 priceDifference = 0; + let arrayOfChanges = []; + for (const subArray of closingPricesForAllStocks) { + + priceDifference = Math.round((subArray[subArray.length -1] - subArray[0]) * 100) /100; + arrayOfChanges.push(priceDifference); + } + + return arrayOfChanges; } /* @@ -64,7 +83,17 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPriceEach = 0; + let arrayOfPrices = []; + let i = 0; + for (const subArray of closingPricesForAllStocks) { + + highestPriceEach = Math.max(...subArray).toFixed(2); + arrayOfPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPriceEach}`); + i++ + + } + return arrayOfPrices; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..5a3a4884 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,12 @@ */ function factorial(input) { - // TODO + let sum = 1; + for (let i = 1; i <= input; i++) { + sum = i * sum; + + } + return sum; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..537ce5cd 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,8 +12,29 @@ function getHighestRatedInEachGenre(books) { // TODO + const children = []; + const nonFiction = []; + const cooking = []; + const highestRatedTitles = []; + for (let i = 0; i < books.length; i++) { + if (books[i].genre === "children") { + children.push(books[i]); + } else if (books[i].genre === "non-fiction") { + nonFiction.push(books[i]); + } else { + cooking.push(books[i]); + } + } + children.sort((a, b) => b.rating - a.rating); + nonFiction.sort((a, b) => b.rating - a.rating); + cooking.sort((a, b) => b.rating - a.rating); + highestRatedTitles.push(children[0].title, nonFiction[0].title, cooking[0].title) + return highestRatedTitles; } +// let numbers = [2, 5, 2, 5, 6, 8, 86,34, 0, 22]; +// console.log(numbers.sort((a, b) => b - a)) + /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ @@ -69,6 +90,7 @@ const BOOKS = [ }, ] +// console.log(getHighestRatedInEachGenre(BOOKS)) /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..7e7934d9 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,13 @@ */ function generateFibonacciSequence(n) { - // TODO + arr = [0]; + num = 1; + for (let i = 0; i < n - 1; i++) { + arr.push(num); + num = num + arr[i]; + } + return arr; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/testFolder/test 12.js b/testFolder/test 12.js new file mode 100644 index 00000000..e1b73e1e --- /dev/null +++ b/testFolder/test 12.js @@ -0,0 +1,15 @@ +function logicalCalc(array, op){ + //your code here + let logic +// for (let i = 0; i < array.length; i++) { + if (op === "AND") { + logic = array.reduce((a, b) => a && b); + } else if (op === "OR") { + logic = array.reduce((a, b) => a || b); + } else if ( op === "XOR") { + logic = array.reduce((a, b) => a != b); + } +// } + return logic +} + diff --git a/testFolder/test.js b/testFolder/test.js new file mode 100644 index 00000000..b8985f49 --- /dev/null +++ b/testFolder/test.js @@ -0,0 +1,13 @@ +function evenNumbers(n) { + let i = 0; + let arr = []; + while (i < n * 2) { + if (i % 2 === 0) { + arr.push(i); + } + i++; + } + console.log(arr); +} + +evenNumbers(10); \ No newline at end of file diff --git a/testFolder/test1.js b/testFolder/test1.js new file mode 100644 index 00000000..db32d29a --- /dev/null +++ b/testFolder/test1.js @@ -0,0 +1,33 @@ +// let arr = [1, 2, 3, 4, 5] +// arr.push(6) + +// console.log(arr) + +const BIRTHDAYS = [ + "January 7th", + "February 12th", + "April 3rd", + "April 5th", + "May 3rd", + "July 11th", + "July 17th", + "September 28th", + "November 15th" +]; + +function findFirstJulyBDay(birthdays) { + let i = 0; + let birthday; + while (i < birthdays.length) { + if (birthdays[i].includes("July")) { + birthday = birthdays[i]; + i++ + } + i++; + } + return birthday +} + +console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" + +// console.log(BIRTHDAYS.find("July")) \ No newline at end of file diff --git a/testFolder/test10.js b/testFolder/test10.js new file mode 100644 index 00000000..cc14779d --- /dev/null +++ b/testFolder/test10.js @@ -0,0 +1,77 @@ +// function generateFibonacciSequence(n) { +// arr = [0]; +// num = 1; +// for (let i = 0; i < n - 1; i++) { +// arr.push(num); +// num = num + arr[i]; +// } +// return arr; +// } + + +// console.log(generateFibonacciSequence(15)) + +function getHighestRatedInEachGenre(books) { + let maxRate = "" + if (books.genre === "children") { + maxRate = books.reduce((max, book) => max.votes > book.votes ? max : book ) + } + return maxRate +} + +const BOOKS = [ + { + title: "The Lion, the Witch and the Wardrobe", + genre: "children", + rating: 4.7 + }, + { + title: "Sapiens: A Brief History of Humankind", + genre: "non-fiction", + rating: 4.7 + }, + { + title: "Nadiya's Fast Flavours", + genre: "cooking", + rating: 4.7 + }, + { + title: "Harry Potter and the Philosopher's Stone", + genre: "children", + rating: 4.8 + }, + { + title: "A Life on Our Planet", + genre: "non-fiction", + rating: 4.8 + }, + { + title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + genre: "cooking", + rating: 4.85 + }, + { + title: "Gangsta Granny Strikes Again!", + genre: "children", + rating: 4.9 + }, + { + title: "Diary of a Wimpy Kid", + genre: "children", + rating: 4.6 + }, + { + title: "BOSH!: Simple recipes. Unbelievable results. All plants.", + genre: "cooking", + rating: 4.6 + }, + { + title: "The Book Your Dog Wishes You Would Read", + genre: "non-fiction", + rating: 4.85 + }, +] + + +getHighestRatedInEachGenre(BOOKS); +// console.log(BOOKS.length) \ No newline at end of file diff --git a/testFolder/test11.js b/testFolder/test11.js new file mode 100644 index 00000000..cbf61e37 --- /dev/null +++ b/testFolder/test11.js @@ -0,0 +1,13 @@ +function well(x){ +let newArr = ""; +newArr = x.toString(); +newArr = newArr.toLowerCase(); + let count = (newArr.match(/good/g) || []).length; + if (count > 2) { + return "I smell a series!" + } else if (count <= 0) { + return "Fail!" + } else return "Publish!" +} + +console.log(well([['bad', 'bAd', 'bad'], ['bad', 'bAd', 'bad'], ['bad', 'bAd', 'bad']])) \ No newline at end of file diff --git a/testFolder/test2.js b/testFolder/test2.js new file mode 100644 index 00000000..36f79b5c --- /dev/null +++ b/testFolder/test2.js @@ -0,0 +1,34 @@ +function temperatureService(city) { + let temparatureMap = new Map(); + + temparatureMap.set('London', 10); + temparatureMap.set('Paris', 12); + temparatureMap.set('Barcelona', 17); + temparatureMap.set('Dubai', 27); + temparatureMap.set('Mumbai', 29); + temparatureMap.set('São Paulo', 23); + temparatureMap.set('Lagos', 33); + + return temparatureMap.get(city); +} + + +// console.log(temperatureService( "Dubai")) + +function getTemperatureReport(cities) { + let arr = []; + for (const element of cities) { + if (temperatureService(element)) { + arr.push(`The temperature in ${element} is ${temperatureService(element)} degrees`); + } + }return arr; + +} + +let usersCities = [ + "London", + "Paris", + "São Paulo" + ] + +console.log(getTemperatureReport(usersCities)) \ No newline at end of file diff --git a/testFolder/test3.js b/testFolder/test3.js new file mode 100644 index 00000000..3e41e415 --- /dev/null +++ b/testFolder/test3.js @@ -0,0 +1,23 @@ +function generateRandomNumber() { + console.log("Generating number..."); + return Math.round(Math.random() * 100); +} + +function getRandomNumberGreaterThan50() { +// let result; +// let i = 0; +// do { if (generateRandomNumber() > 50) { +// return generateRandomNumber() >50} +// i++; + +// } +// while ( getRandomNumberGreaterThan50() > 50); +// return getRandomNumberGreaterThan50() +// +return generateRandomNumber() + 50; + +} + + + +console.log(getRandomNumberGreaterThan50()) \ No newline at end of file diff --git a/testFolder/test4.js b/testFolder/test4.js new file mode 100644 index 00000000..2acb7ac3 --- /dev/null +++ b/testFolder/test4.js @@ -0,0 +1,84 @@ +// function potentialHeadlines(allArticleTitles) { +// let arr = []; +// for (const element of allArticleTitles) { +// if (element.length <= 65) { +// arr.push(element); +// } +// }return arr; +// } + + + +// function headlinesWithNumbers(allArticleTitles) { +// let array = [] +// for (const element of allArticleTitles){ +// if ( /\d/.test(element)) { +// array.push(element); +// } +// } return array; +// } + +// function titleWithFewestWords(allArticleTitles) { +// const allSentences = allArticleTitles[0]; +// let smallHeadline = "" +// for (let i = 0; i < allArticleTitles.length; i++) { +// if (allSentences[i].length > 0 && allSentences[i].length < 2) { +// if(allSentences[i].length > smallHeadline.length) { +// smallHeadline = allSentences[i]; +// return smallHeadline +// } + +// } +// } +// // TODO +// } + + +// function titleWithFewestWords(allArticleTitles){ + // let titleLength = []; + // let result = ""; + // for (let i = 0; i < allArticleTitles.length; i++){ + // let titleArray = allArticleTitles[i].split(" "); + // let articleLength = titleArray.length; + // if(result.length < articleLength.length){ + // result = allArticleTitles[articleLength]; + // } + // } + // return result; + +// for(const element of allArticleTitles) { +// if (allArticleTitles[0] > 1) { +// smallArray += allArticleTitles[i] +// } +// } +function titleWithFewestWords(allArticleTitles){ + // let titleLength = []; + let result = " "; + for (let i = 0; i < allArticleTitles.length; i++){ + let titleArray = allArticleTitles[i].split(" "); + let articleLength = titleArray.length; + if(result.length < articleLength){ + result = allArticleTitles[i]; + } + } + return result; +} + +// } + + + + + +let arr = ["Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day aa aa aa aa aa aa aa aa aa aa aa", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal",] + +console.log(titleWithFewestWords(arr)) \ No newline at end of file diff --git a/testFolder/test5.js b/testFolder/test5.js new file mode 100644 index 00000000..cf812f1a --- /dev/null +++ b/testFolder/test5.js @@ -0,0 +1,24 @@ +function titleWithFewestWords(allArticleTitles) { + let arrLength = []; + let smallestNumPosition; + let index; + for (const element of allArticleTitles) { + arrLength.push(element.split(" ").length); + } + smallestNumPosition = Math.min(...arrLength); + index = arrLength.indexOf(smallestNumPosition) + console.log(allArticleTitles[index]); +} + +let arr = ["Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day aa aa aa aa aa aa aa aa aa aa aa", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal",] + + titleWithFewestWords(arr) \ No newline at end of file diff --git a/testFolder/test6.js b/testFolder/test6.js new file mode 100644 index 00000000..dac201bd --- /dev/null +++ b/testFolder/test6.js @@ -0,0 +1,27 @@ +function averageNumberOfCharacters(allArticleTitles) { + let arrNumOfCharacters = []; + let total = 0; + let average = 0; + for (const element of allArticleTitles) { + arrNumOfCharacters.push(element.trim().length); + } + for (const element of arrNumOfCharacters) { + total = total + element; + } + average = total / arrNumOfCharacters.length; + return Math.round(average); +} + + +let arr = ["Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day aa aa aa aa aa aa aa aa aa aa aa", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal",] + + averageNumberOfCharacters(arr) \ No newline at end of file diff --git a/testFolder/test7.js b/testFolder/test7.js new file mode 100644 index 00000000..24163e73 --- /dev/null +++ b/testFolder/test7.js @@ -0,0 +1,58 @@ +const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; + +const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL + [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA +]; + + + +// function getAveragePrices(closingPricesForAllStocks) { +// let total = 0; +// let average = 0; +// arrayOfAverages = []; +// for (const subArray of closingPricesForAllStocks) { +// for (const element of subArray) { +// total = total + element; +// } +// average = total / subArray.length; +// total = 0; +// arrayOfAverages.push(Math.round(average *100)/100); +// } +// console.log(arrayOfAverages); +// } + +// getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) + +// function getPriceChanges(closingPricesForAllStocks) { +// let priceDifference = 0; +// let arrayOfChanges = []; +// for (const subArray of closingPricesForAllStocks) { + +// priceDifference = Math.round((subArray[subArray.length -1] - subArray[0]) * 100) /100 +// arrayOfChanges.push(priceDifference); +// } + +// console.log(arrayOfChanges); +// } + +// getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) + +function highestPriceDescriptions(closingPricesForAllStocks, stocks) { + let highestPriceEach = 0; + let arrayOfPrices = []; + let i = 0; + for (const subArray of closingPricesForAllStocks) { + + highestPriceEach = Math.max(...subArray).toFixed(2); + arrayOfPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPriceEach}`); + i++ + + } + console.log(arrayOfPrices) +} + +highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) \ No newline at end of file diff --git a/testFolder/test8.js b/testFolder/test8.js new file mode 100644 index 00000000..7bb57a1b --- /dev/null +++ b/testFolder/test8.js @@ -0,0 +1,20 @@ +function generateRandomNumber() { + console.log("Generating number..."); + return Math.round(Math.random() * 100); +} + +function getRandomNumberGreaterThan50() { + let i = 0; + let arr = []; + do { + arr.push(generateRandomNumber()); + if (arr[i] > 50) { + arr = arr[i]; + } + i++; + } + while (i <= arr.length); + return arr +} + +console.log(getRandomNumberGreaterThan50()); \ No newline at end of file diff --git a/testFolder/test9.js b/testFolder/test9.js new file mode 100644 index 00000000..3d99272f --- /dev/null +++ b/testFolder/test9.js @@ -0,0 +1,14 @@ +// function factorial(input) { +// let sum =1; +// for (let i = 1; i <= input; i++) { +// sum = i * sum; + +// } +// return sum; +// } + +// console.log(factorial(10)); + +var haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]; + +console.log(haystack_1.indexOf("needle") + 1) \ No newline at end of file