diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..d20baa2a 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,7 +12,7 @@ // Example 1 let a; console.log(a); - +/* we didn't assign any value for our variable */ // Example 2 function sayHello() { @@ -21,6 +21,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +/* there is no parameter in our function */ // Example 3 @@ -29,8 +30,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +/* we didn't give any value to our parameter in function */ // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +/* there is no index number 3 in this array we have only 0,1,2 */ diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..d6581b72 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,15 @@ */ function evenNumbers(n) { - // TODO + let number = 0; + let arr = []; + while (n > 0) { + arr.push(number); + number += 2; + n--; + } + arr = arr.join(","); + console.log(arr); } 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..e87b0ffa 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,13 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let currentIndex = 0; + while (currentIndex < birthdays.length) { + if (birthdays[currentIndex].includes("July")) { + return birthdays[currentIndex]; + } + currentIndex++; + } } 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..dc716da3 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,14 @@ function evenNumbersSum(n) { // TODO + let number = 0; + let total = 0; + do { + total += number; + number += 2; + n--; + } while (n > 0); + 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..25c05c09 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++; -} +// let i = 0; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } // The output shouldn't change. +for (i = 0; i < 26; i++) { + console.log(String.fromCharCode(97 + i)); +} \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..2002a071 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 (i = 0; i <= 5; 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..af6db988 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -4,13 +4,20 @@ // TODO Use a for-of loop to output each of the tube stations below. let tubeStations = [ - "Aldgate", - "Baker Street", - "Picadilly Circus", - "Oxford Street", - "Tottenham Court Road" + "Aldgate", + "Baker Street", + "Picadilly Circus", + "Oxford Street", + "Tottenham Court Road", ]; +for (let el of tubeStations) { + console.log(el); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (let char of str) { + console.log(char.toUpperCase()) +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..1effe46c 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + // TODO + let result = []; + for (let i = 0; i < cities.length; i++) { + result.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`); + } + return result; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..2b48cf56 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let number = generateRandomNumber(); + do { + number = generateRandomNumber(); + } while (number < 50); + return number; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..bbde457a 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,15 +6,36 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + return allArticleTitles.filter(items => items.length <= 65); +// let headlines = []; + +// for(let title of allArticleTitles) { +// if(title.length <= 65) { +// headlines.push(title); +// } +// } + +// return headlines; } + /* The editor of the FT likes short headlines with only a few words! 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 + //TODO + let arr = []; + for (let i = 0; i < allArticleTitles.length; i++) { + arr.push(allArticleTitles[i].split(" ").length); + } + // return allArticleTitles[arr.indexOf(Math.min(...arr))]; + const fewestWords = Math.min(...arr); + const indexOfArticle = arr.indexOf(fewestWords); + const articleTitle = allArticleTitles[indexOfArticle]; + return articleTitle; + } /* @@ -24,6 +45,18 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + // let arrayNum = [] + // for (let el of ARTICLE_TITLES) { + // for (let ele of el){ + // if (ele.includes(Number)) { + // return arrayNum.push(ele.includes(Number)); + // } + // } + // return arrayNum + // } + return allArticleTitles.filter((element) => + [...element].find((number) => number >= "0" && number <= "9") + ); } /* @@ -32,6 +65,11 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let sum = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + sum += allArticleTitles[i].length; + } + return Math.round(sum / allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..4f915fd3 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,10 +33,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ +function calculateAverage(arr) { + let sum = 0; + for (let price of arr) { + sum += price; + } + const average = sum / arr.length; + return average; +} + + function getAveragePrices(closingPricesForAllStocks) { - // TODO + const myArr = []; + for (let stock of closingPricesForAllStocks) { + const average = calculateAverage(stock); + const formattedAverage = Number(average.toFixed(2)); + myArr.push(formattedAverage); + } + return myArr; } + /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which @@ -48,7 +65,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 myArra = []; + for (let price of closingPricesForAllStocks) { + let firstDay = price[0]; + let lastDay = price[price.length - 1]; + let difference = lastDay - firstDay; + myArra.push(Number(difference.toFixed(2))); + } + return myArra; } /* @@ -65,6 +90,17 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let arr = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + arr.push( + `The highest price of ${stocks[ + i + ].toUpperCase()} in the last 5 days was ${Math.max( + ...closingPricesForAllStocks[i] + ).toFixed(2)}` + ); + } + return arr; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..6acfa989 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,11 @@ function factorial(input) { // TODO + let sum = 1 + for (let i = 1; i <= input; i++){ + sum *= i + } + 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..547d005d 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,22 @@ function getHighestRatedInEachGenre(books) { // TODO + const result = books.reduce((acc, cur) => { + const groupByGenre = cur.genre; + if (!acc[groupByGenre]) { + acc[groupByGenre] = []; + } + acc[groupByGenre].push(cur); + return acc; + }, {}); + const genreName = Object.keys(result); + const arr = []; + for (let i = 0; i < genreName.length; i++) { + arr.push( + result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title + ); + } + return arr; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..19534567 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,11 @@ function generateFibonacciSequence(n) { // TODO + let fibo = [0, 1]; + for (let i = 0; i < n - 2; i++) { + fibo.push(fibo[i] + fibo[i + 1]); + } + return fibo; } /* ======= TESTS - DO NOT MODIFY ===== */