From 3e9bbf2a5651c8686798f344c7bd1759ff124205 Mon Sep 17 00:00:00 2001 From: Karla Grajales Date: Sat, 31 Dec 2022 01:13:18 +0000 Subject: [PATCH 1/3] Mandatory done --- 2-mandatory/1-weather-report.js | 12 +++- 2-mandatory/2-retrying-random-numbers.js | 15 +++- 2-mandatory/3-financial-times.js | 90 +++++++++++++++++++++++- 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..3f9df8ca 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,11 +11,19 @@ - Hint: you can call the temperatureService function from your function */ +// function getTemperatureReport(cities) { +// // TODO +// } function getTemperatureReport(cities) { - // TODO + let cityWeather = []; + for (let i = 0; i < cities.length; i++) { + cityWeather.push( + `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees` + ); + } + return cityWeather; } - /* ======= 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..32046571 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -1,16 +1,25 @@ /* - In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50. + In the below example, we want to keep calling generateRandomNumber + until we get a value that is > 50. Implement this using a do-while loop. */ // This function shouldn't be changed function generateRandomNumber() { console.log("Generating number..."); - return Math.round(Math.random() * 100); + return Math.round(Math.random() * 100); } + function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + // TODO - implement using a do-while loo + let randomNumber; + do { + randomNumber = generateRandomNumber(); + + } while (randomNumber < 50); //we will run this code, only if we received a value less than 50. + return randomNumber; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..199be927 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,18 +5,32 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let shorTitles = []; + for (const title of allArticleTitles) { + if (title.length <= 65){ + shorTitles.push(title); + } + } + return shorTitles; } + /* 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) */ + + +// If you want to use the Math.min approach, first extract he lengths of each word and then pass those to the Math.max. + function titleWithFewestWords(allArticleTitles) { - // TODO + } + + + /* The editor of the FT has realised that headlines which have numbers in them get more clicks! Implement the function below to return a new array containing all the headlines which contain a number. @@ -34,7 +48,79 @@ function averageNumberOfCharacters(allArticleTitles) { // TODO } +/* + Imagine you are working on the Financial Times web site! They have a + list of article titles stored in an array. + 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) { +// let arr =[]; +// for (let article of allArticleTitles){ +// if (article.length <= 65){ +// arr.push(article) +// } +// } +// return arr; +// } + +// /* +// 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) { +// let arr = []; +// for (let i = 0; i < allArticleTitles.length; i++) { +// arr.push(numberOfWords(allArticleTitles[i])); +// } +// return allArticleTitles[arr.indexOf(Math.min(...arr))]; +// } + +// function numberOfWords(title) { +// console.log(`title: ${title}`); +// let words = title.split(' '); +// console.log(`words: ${words}`); +// return words.length; +// } +// // new link in chat!!! +// /*let +// on The editor of the FT has realised that headlines which have numbers in them get more clicks!n +// Implement the function below to return a new array containing all the headlines which ctain a number. +// (Hint: remember that you can also loop through the characters of a string if you need to) +// */ +// function headlinesWithNumbers(allArticleTitles) { +// // TODO +// let arr= []; +// for (let article of allArticleTitles){ +// for (let char of article){ +// if (char>="0" && char<="9"){ +// arr.push (article); +// break; +// } + +// } +// } +// return arr +// } + +// /* +// 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 sum = 0; +// for (let article of allArticleTitles){ +// sum += article.length; + +// } + +// return Math.round(sum / allArticleTitles.length) +// } +// /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ From 49d75e76641c3126b72be677b121ed88d75cf5d0 Mon Sep 17 00:00:00 2001 From: Karla Grajales Date: Tue, 3 Jan 2023 09:05:26 +0000 Subject: [PATCH 2/3] exercises folder A done --- 1-exercises/A-undefined/exercise.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..3840d745 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,27 +10,32 @@ */ // Example 1 -let a; -console.log(a); +// let a; +// console.log(a); -// Example 2 -function sayHello() { - let message = "Hello"; -} +// // Example 2 +// function sayHello(a) { +// let message = "Hello2"; +// return message +// } -let hello = sayHello(); -console.log(hello); +// let hello = sayHello(); +// console.log(hello); // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); // this will return hello and then undefined because we did not declare + // of the variable user. } sayHelloToUser(); + // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// \ No newline at end of file From d5c7bb120a5340ecd9e7b99ae59aceadc0b0cc46 Mon Sep 17 00:00:00 2001 From: Karla Grajales Date: Tue, 3 Jan 2023 21:19:30 +0000 Subject: [PATCH 3/3] exercises done --- 1-exercises/A-undefined/exercise.js | 28 ++++++++++--------- 1-exercises/B-while-loop/exercise.js | 15 +++++++--- .../C-while-loop-with-array/exercise.js | 15 ++++++++-- 1-exercises/D-do-while/exercise.js | 15 ++++++++-- 1-exercises/E-for-loop/exercise1.js | 20 +++++++++---- 1-exercises/E-for-loop/exercise2.js | 15 +++++++++- 1-exercises/F-for-of-loop/exercise.js | 23 ++++++++++++++- 7 files changed, 103 insertions(+), 28 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 3840d745..b6136b05 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,33 +9,35 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 -// let a; -// console.log(a); +//the variable did not contain any value. for this reason will be undefined +//Example 1 +let a; +console.log(a); -// // Example 2 -// function sayHello(a) { -// let message = "Hello2"; -// return message -// } -// let hello = sayHello(); -// console.log(hello); +//this fuction did not have return, so when it's called will be undefined +// Example 2 +function sayHello(a) { + let message = "Hello2"; +} + +let hello = sayHello(); +console.log(hello); +// this will return hello and then undefined because we did not declare of the variable user. // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); // this will return hello and then undefined because we did not declare - // of the variable user. + console.log(`Hello ${user}`); } sayHelloToUser(); +//this array contain 3 items but the index start from 0 and will display until index[2]=3 // Example 4 let arr = [1,2,3]; console.log(arr[3]); -// \ 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..c3565e00 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,16 @@ */ function evenNumbers(n) { - // TODO + let result = [] + let i = 0; + while(i < n){ + result.push(i * 2) + i++ +} +return result.join() //with join() we can display the length of the chain in te same line } -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 +console.log(evenNumbers(3)); // should output 0,2,4 +console.log(evenNumbers(0)); // should output nothing +console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18 +// console.log(evenNumbers(3)) diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..7142a2b3 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,19 @@ const BIRTHDAYS = [ "November 15th" ]; -function findFirstJulyBDay(birthdays) { - // TODO +function findFirstJulyBDay(birthday) { + let i = 0 + while(i < birthday.length){ + if(birthday[i].startsWith('July')){ + return birthday[i]; + } + i++ + } } +//this function will find the first word that start with July and will return it. console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" + +// String.prototype.startsWith() +// The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..fff7b967 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,20 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + + let i = 0; + do { + sum += (i * 2); + i++; + } while(i < n); + return sum + } console.log(evenNumbersSum(3)); // should output 6 console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file +console.log(evenNumbersSum(10)); // should output 90 + + + diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..3b747380 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,19 @@ // 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(let i = 0; i < 26; i++){ + console.log(String.fromCharCode(97 + i)) + // return String.fromCharCode(97 + i) +} + +/*String.fromCharCode() +The String.fromCharCode() static method returns a string created from the specified sequence of UTF-16 code units. +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode*/ \ 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..a13507ed 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,7 +28,20 @@ const AGES = [ // TODO - Write for loop code here -/* +for(let i = 0; i < WRITERS.length && AGES.length; i++){ + console.log(WRITERS[i] + " is " + AGES[i] + " years old") +} + +// ==================other option with interpolation=============== + +// for(let i = 0; i < WRITERS.length; i++) { +// // Because we want to retrieve the element at the same index for both arrays, we can re-use the same variable i +// // We are also making use of string interpolation here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals +// console.log(`${WRITERS[i]} is ${AGES[i]} years old`); +// } +/* + + The output should look something like this: Virginia Woolf is 59 years old diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..dc871190 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -1,5 +1,6 @@ /* - 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. @@ -11,6 +12,26 @@ let tubeStations = [ "Tottenham Court Road" ]; +for(let station of tubeStations) { + console.log(station); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for(let letter of str) { + console.log(letter.toUpperCase()); +} + +let array = "a1, b2, c4, d5, g7, k8"; + +for(let letter of array) { + console.log(letter.toUpperCase()); +} + +let chain ="i am a string"; + +for(let letter of chain) { + console.log(letter.toUpperCase()); +} \ No newline at end of file