-
-
Notifications
You must be signed in to change notification settings - Fork 279
WM4_AlessandraSousa_Javascript-core-1_Week3 #113
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| /* | ||
| while loops can be useful when you want to execute some code as long as some condition is true. | ||
|
|
||
| Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. | ||
| Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-separated string. | ||
| The list of numbers should start with 0. n is being passed in as a parameter. | ||
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| let array = [] | ||
| let i = 0; | ||
| while (i < n) { | ||
| array.push(i * 2); | ||
| i++ | ||
| } | ||
| console.log(array.toString()); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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 | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,13 @@ const BIRTHDAYS = [ | |
| ]; | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| let i = 0; | ||
| while (i < birthdays.length) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! |
||
| if (birthdays[i].includes("July")){ | ||
| return birthdays[i]; | ||
| } | ||
| i++ | ||
| } | ||
| } | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,13 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| let sum = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remember about correct code formatting. It is making your life easier when You are coding and reading code. |
||
| let i = 0; //starting from zero | ||
| do { | ||
| sum = i * (i + 1); //formula for the sum of consecutive even numbers: n(n+1) or JSshort: sum+= i * 2 | ||
| i++; | ||
| } while (i < n); | ||
| return sum; | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,11 @@ | |
|
|
||
|
|
||
| // 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++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| console.log(String.fromCharCode(97 + i)); | ||
| i++; | ||
| } | ||
| // The output shouldn't change. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,9 +25,9 @@ const AGES = [ | |
| 63, | ||
| 49 | ||
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for (let i = 0; i < WRITERS.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct indentation :) |
||
| console.log(`${WRITERS[i]} is ${AGES[i]} years old`); | ||
| } | ||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| Imagine we're making a weather app! | ||
|
|
||
| We have a list of cities that the user wants to track. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temparature. | ||
| We also already have a temperatureService function which will take a city as a parameter and return a temperature. | ||
|
|
||
| Implement the function below: | ||
| - take the array of cities as a parameter | ||
|
|
@@ -12,24 +12,31 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| } | ||
| let citiesTemp = []; | ||
| for (city of cities) { | ||
| let temp = temperatureService(city); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused variable - next time, please remove it or use in string interpolation :) |
||
| citiesTemp.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); | ||
| } | ||
| return citiesTemp; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| 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); | ||
| let temperatureMap = new Map(); | ||
|
|
||
| temperatureMap.set('London', 10); | ||
| temperatureMap.set('Paris', 12); | ||
| temperatureMap.set('Barcelona', 17); | ||
| temperatureMap.set('Dubai', 27); | ||
| temperatureMap.set('Mumbai', 29); | ||
| temperatureMap.set('São Paulo', 23); | ||
| temperatureMap.set('Lagos', 33); | ||
|
|
||
| return temparatureMap.get(city); | ||
| return temperatureMap.get(city); | ||
| } | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have nicely done a while loop!