diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..af9feb87 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,17 +11,19 @@ // Example 1 let a; -console.log(a); +console.log(a); +// a is declared as a variable. But no value has been assigned. // Example 2 function sayHello() { let message = "Hello"; } +// sayHello function created. But it has no parameter in it. let hello = sayHello(); console.log(hello); - +// hello variable has a value which is sayHello function. But this function has no parameter in it. // Example 3 function sayHelloToUser(user) { @@ -29,8 +31,11 @@ function sayHelloToUser(user) { } sayHelloToUser(); +// user declared as a parameter. But when calling a function, an argument is needed not to get undefined result. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// The index is starting from 0 in array. That`s why the last index of array "arr" is [2]. +// So there is no value for arr[3]. So, arr[3] will be undefined. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8427ff4a 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,20 @@ */ function evenNumbers(n) { - // TODO + let arr = []; + let i = 0; + 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 + + diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..c119b909 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -1,7 +1,9 @@ /* 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. + 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. */ const BIRTHDAYS = [ @@ -17,7 +19,11 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let counter = 0; + while(birthdays[counter].includes("July") != true) { + counter++; + } + return birthdays[counter]; } 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..d06cc6d6 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -1,13 +1,25 @@ /* - Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop. - - If the condition in a while loop is initially false, the body of the loop will never execute - - But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once + Sometimes when using loops, we'll want to execute the body of the loop at least once. + We can make sure this happens by using a do-while loop. + - If the condition in a while loop is initially false, + the body of the loop will never execute + - But in a do-while loop, because the condition is checked after the body, + we know that it will always execute at least once - Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) + Using a do-while loop, write a function which returns + the sum of the first n even numbers (starting from 0) */ function evenNumbersSum(n) { - // TODO + let i = 0; + let num = 0; + let sum = 0; + do { + sum += num; + num += 2; + i++; + } while (i < n); + return sum; } 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..91a53072 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -1,14 +1,20 @@ /* - for loops can be useful when we already know exactly how many times we want to loop. + for loops can be useful when we already know exactly how many times + we want to loop. Change the while loop below into a for loop. */ // Change the below code to use a for loop instead of a while loop. -let i = 0; +/*let i = 0; while(i < 26) { console.log(String.fromCharCode(97 + i)); i++; } +*/ +for (let index = 0; index < 26; index++) { + console.log(String.fromCharCode(97 + index)); + +} // 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..b97ace0b 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -1,5 +1,6 @@ /* - for loops can be useful when we already know exactly how many times we want to loop. + for loops can be useful when we already know exactly how many times + we want to loop. Below we have 2 arrays which have exactly the same number of values. - The first array is a list of writers @@ -27,6 +28,10 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let index = 0; index < WRITERS.length; index++) { + console.log(`${WRITERS[index]} is ${AGES[index]} 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..826fa088 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -1,5 +1,7 @@ /* - A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). + A for-of loop is a easy and way of looping through + the elements of an array, string or any other + "iterable object" (think sequence of elements). */ // TODO Use a for-of loop to output each of the tube stations below. @@ -10,7 +12,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (const str of tubeStations) { + console.log(str); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (const iterator of str) { + console.log(iterator.toUpperCase()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..6f1e2e70 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let cityNameAndDegree = []; + for (const temperature of cities) { + cityNameAndDegree.push(`The temperature in ${temperature} is ${temperatureService(temperature)} degrees`); + } + return cityNameAndDegree; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..dc778ddc 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 num; + do { + num = generateRandomNumber(); + } while (num <= 50); + return num; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..dcfcf7c6 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 publishedTitle = []; + for (let titleCharacter of allArticleTitles ) { + if (titleCharacter.length <= 65){ + publishedTitle.push(titleCharacter); + } + } + return publishedTitle; } /* @@ -14,7 +20,19 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let wordLength = 100; + let index = 0; + + allArticleTitles.forEach((article, i) => { + let split = article.split(" "); + + if (split.length < wordLength) { + wordLength = split.length; + index = i; + } + }); + + return allArticleTitles[index]; } /* @@ -23,7 +41,15 @@ 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 newHeadlinesWithNumbers = []; + for (let i = 0; i < allArticleTitles.length; i++) { + for (let j = 0; j < allArticleTitles[i].length; j++) { + if (allArticleTitles[i][j] === "$") { + newHeadlinesWithNumbers.push(allArticleTitles[i]) + } + } + } + return newHeadlinesWithNumbers; } /* @@ -31,7 +57,15 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let newNumbers = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + for (let j = 0; j < allArticleTitles[i].length; j++) { + newNumbers++; + + } + } + return Math.round(newNumbers / allArticleTitles.length); + }