diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..8c5e3680 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,13 +10,14 @@ */ // Example 1 -let a; +let a = "ahmed"; // in this example we shuold add value to the variable. console.log(a); // Example 2 function sayHello() { let message = "Hello"; + return message; //in the examplewe need to write return to avoide the undefined. } let hello = sayHello(); @@ -28,9 +29,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser("Omer"); // for this example we miss to add value when we call the function. // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]);// because the arr has 3 item and the firest item start with 0 and 1 and 2 but there is no for item. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..5ce5f5fa 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,13 @@ function evenNumbers(n) { // TODO + let myArray = []; + let i = 0; + while (n > myArray.length) { + myArray.push(i); + i += 2; + } +console.log(myArray); } 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..311bce58 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -1,7 +1,8 @@ /* Loops can be useful when working with arrays. In the below example, imagine we've defined an array holding the birthdays of your closest friends. - Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function. + Use a while loop to search through the array until you find the first birthday in + July, then return that birthday from the function. */ const BIRTHDAYS = [ @@ -18,6 +19,12 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + + let num = 0; + while (num < birthdays.length) { + if (birthdays[num].includes("July")) return birthdays[num]; + num++; + } } 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..e5243a15 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,7 +8,18 @@ function evenNumbersSum(n) { // TODO -} + const ourArray = []; + let i = 0; + + do { + ourArray.push(i); + i++; + } while (i < 10); + return i; + } + + console.log(sumEvens(ourArray)); + 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..ded7e9d1 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,10 @@ // 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) +for (let i = 0; i < 26; i++) { console.log(String.fromCharCode(97 + i)); - 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..69c5f1d7 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,12 @@ 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..4198478d 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,13 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; +for (i of tubeStations) { + console.log(i); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (i of str) { + console.log(i); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..85816abb 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,16 @@ function getTemperatureReport(cities) { // TODO + let report = []; + for(let city of cities) { + let temperature = temperatureService(city); + report.push(`The temperature in ${city} is ${temperature} degrees`); + } + return report; } + /* ======= 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..3ff57831 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,13 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let generatedNumber; + + do { + generatedNumber = generateRandomNumber(); + } while(generatedNumber <= 50); + + return generatedNumber; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..52cf1f4a 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,15 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let headlines = []; + + for(let title of allArticleTitles) { + if(title.length <= 65) { + headlines.push(title); + } + } + + return headlines; } /* @@ -15,6 +24,22 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let fewestWordsSoFar; + let titleWithFewestWords; + + for(let title of allArticleTitles) { + + // working out the number of words in the title by splitting on the space character + // this will generate an array. Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split + let numWords = title.split(' ').length; + + if(fewestWordsSoFar === undefined || numWords < fewestWordsSoFar) { + fewestWordsSoFar = numWords; + titleWithFewestWords = title; + } + } + + return titleWithFewestWords; } /* @@ -24,6 +49,27 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let articlesWithNumbers = []; + + for(let title of allArticleTitles) { + // Making use of the new function created below + if(doesTitleContainANumber(title)) { + articlesWithNumbers.push(title); + } + } + + return articlesWithNumbers; +} + +// Creating another function to help break this problem down into smaller parts +function doesTitleContainANumber(title) { + for(let character of title) { + if(character >= '0' && character <= '9') { + return true; + } + } + + return false; } /* @@ -32,6 +78,13 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let totalCharacters = 0; + + for(let title of allArticleTitles) { + totalCharacters += title.length; + } + + return Math.round(totalCharacters / allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..54bd26bd 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let averages = []; + + for(let pricesForStock of closingPricesForAllStocks) { + averages.push(getAveragePricesForStock(pricesForStock)); + } + + return averages; +} + +function getAveragePricesForStock(pricesForStock) { + let total = 0; + + for(let price of pricesForStock) { + total += price; + } + + return roundTo2Decimals(total / pricesForStock.length); +} + +function roundTo2Decimals(num) { + return Math.round(num * 100) / 100; } /* @@ -48,7 +69,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 + // TODOlet changes = []; + + for(let pricesForStock of closingPricesForAllStocks) { + changes.push(getPriceChangeForStock(pricesForStock)) + } + + return changes; +} + +function getPriceChangeForStock(pricesForStock) { + let priceChange = pricesForStock[pricesForStock.length - 1] - pricesForStock[0] + return roundTo2Decimals(priceChange); } /* @@ -65,6 +97,39 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = getHighestPrice(closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; +} + +function getHighestPrice(pricesForStock) { + // initialising to 0, as we're expecting this value to be overriden by the first price in the array + let highestPriceSoFar = 0; + + for(let price of pricesForStock) { + // if this price is higher than the highest price we've seen so far, it becomes the new highest price + if(price > highestPriceSoFar) { + highestPriceSoFar = price; + } + } + + return highestPriceSoFar; +} + +function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) { + let descriptions = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) { + let highestPrice = Math.max(...closingPricesForAllStocks[i]); + descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`); + } + + return descriptions; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..751a3d6a 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,15 @@ function factorial(input) { // TODO + let result = 1; + + let i = input + do { + result *= i; + i--; + } while(i > 0); + + return result; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..cbac0e3b 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,20 @@ function getHighestRatedInEachGenre(books) { // TODO + let highestRated = {}; + + for(let book of books) { + // if this is the first time we're seeing this genre OR the rating we've seen is not as high as the current book + if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) { + // then this book is now the highest rated in the genre so far + highestRated[book.genre] = book; + } + } + + // Here we just want to get the highest rated books (the values of the object) + // and then get the title for each one + return Object.values(highestRated) + .map(book => book.title); } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..ac834583 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,19 @@ function generateFibonacciSequence(n) { // TODO + let fibonacciSequence = []; + + // just initialise the first 2 values in the sequence + fibonacciSequence[0] = 0; + fibonacciSequence[1] = 1; + + // the remaining numbers can be calculated + for (i = 2; i < n; i++) { + // each number is equal to the sum of the previous 2 numbers + fibonacciSequence[i] = fibonacciSequence[i - 2] + fibonacciSequence[i - 1]; + } + + return fibonacciSequence; } /* ======= TESTS - DO NOT MODIFY ===== */