-
-
Notifications
You must be signed in to change notification settings - Fork 279
NW5-Manchester-Khadar-M-Dagal JS1-W3 #152
base: main
Are you sure you want to change the base?
Changes from all commits
3ae4345
cfdc33a
b06fea0
1658be7
bd3d234
b61902f
340bc4d
db85f48
29fea01
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,12 +1,18 @@ | ||
| /* | ||
| 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-seperated string. | ||
| The list of numbers should start with 0. n is being passed in as a parameter. | ||
| */ | ||
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| // TODO | ||
| let i = 0; | ||
| while (i % 2 === 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. So, this looks good from a distance but it doesn't quite work as intended. The loop will only run once whatever number you pass to the function. Can you see why? |
||
| console.log(); | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 = [ | ||
|
|
@@ -16,8 +18,42 @@ const BIRTHDAYS = [ | |
| "November 15th" | ||
| ]; | ||
|
|
||
| console.log(); | ||
|
|
||
| function findFirstJulyBDay(birthdays) { | ||
| // TODO | ||
| // TODO | ||
| // lets sort the array first a-z | ||
|
|
||
| birthdays.sort(); | ||
| let i = 0; | ||
| while (i < birthdays.length) { | ||
| if (birthdays[i].includes('July')) { | ||
| return birthdays[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. Works, good work, bit much commnented out code here I would say See: https://syllabus.codeyourfuture.io/guides/code-style-guide#dont-leave-lots-of-commented-out-code |
||
| } | ||
| i++ | ||
| } | ||
| /* | ||
| using for loop | ||
|
|
||
|
|
||
| BIRTHDAYS.sort(); | ||
| for (let i = 0; i < birthdays.length; i++){ | ||
| if (birthdays[i].includes('July')) { | ||
| return birthdays[i] | ||
| } | ||
|
|
||
| } | ||
| */ | ||
| } | ||
|
|
||
| console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" | ||
|
|
||
| /* | ||
| let i = 0; | ||
| while (birthdays[i] === 'July 11th') { | ||
| console.log(birthdays[i]) | ||
| i++; | ||
| } | ||
|
|
||
| return birthdays[i] = birthdays[i]; | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,44 @@ | ||
| /* | ||
| 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. | ||
| 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 | ||
| - 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) | ||
| 1 Using a do-while loop, | ||
| 2 write a function which returns | ||
| *the sum of | ||
| *the first n even numbers (starting from 0) | ||
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
|
|
||
| let sum = 0; | ||
| let arr = []; | ||
| for (let i = 0; i < n; i++){ | ||
| if (i >0 && i % 2 === 0) { | ||
| arr.push(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. This is decent code but it doesn't "return the sum of the first n even numbers (starting from 0)" |
||
| for (let j = 0; j < arr; j++) { | ||
| console.log(arr[j]); | ||
| } | ||
| // console.log(sum) | ||
| } | ||
| // return sum; | ||
| } | ||
| evenNumbersSum(10); | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
| console.log(evenNumbersSum(0)); // should output 0 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| /* | ||
| if (n[i] % 2 === 0) { | ||
| let arr = []; | ||
| arr.push(n[i]); | ||
| } | ||
| */ | ||
| //evenNumbersSum(10); | ||
| // console.log(n) | ||
| /* | ||
| console.log(evenNumbersSum(3)); should output 6 | ||
| console.log(evenNumbersSum(0)); should output 0 | ||
| console.log(evenNumbersSum(10)); should output 90 | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,14 @@ | |
| 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; | ||
| while(i < 26) { | ||
| console.log(String.fromCharCode(97 + i)); | ||
| i++; | ||
| // let i = 0; | ||
| // while(i < 26) { | ||
|
|
||
| // i++; | ||
| // } | ||
|
|
||
| for (let i = 0; i < 26; i++) { | ||
| console.log(String.fromCharCode(97 + 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. Good work |
||
| } | ||
| // The output shouldn't change. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,9 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for (let i = 0; i < WRITERS.length; i++){ | ||
| console.log(`${WRITERS[i]} ${AGES[i]} years old`); | ||
|
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 work |
||
| } | ||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,20 @@ | |
|
|
||
| // TODO Use a for-of loop to output each of the tube stations below. | ||
|
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. All good here! |
||
| let tubeStations = [ | ||
| "Aldgate", | ||
| "Baker Street", | ||
| "Picadilly Circus", | ||
| "Oxford Street", | ||
| "Tottenham Court Road" | ||
| "Aldgate", | ||
| "Baker Street", | ||
| "Picadilly Circus", | ||
| "Oxford Street", | ||
| "Tottenham Court Road", | ||
| ]; | ||
|
|
||
| for (let x of tubeStations) { | ||
| console.log(x); | ||
| } | ||
|
|
||
|
|
||
| // TODO Use a for-of loop to capitalise and output each letter in the string seperately. | ||
| let str = "codeyourfuture"; | ||
| for (let i of str) { | ||
| console.log(i.toUpperCase()); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,29 @@ | |
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| // TODO | ||
| return cities.map(city => `The temperature in ${city} is ${temperatureService(city)} degrees`) | ||
|
|
||
| } | ||
|
|
||
| // console.log(getTemperatureReport(["London", "Paris", "São Paulo"])); | ||
|
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. Bit too much commented out code here but the tests pass and the non commented out code looks good, so good work |
||
|
|
||
| /* | ||
| for (let city of cities) { | ||
| return `The temperature in ${temperatureService(city)} degrees` | ||
| } | ||
| cities.forEach((element, i) => { | ||
| console.log(element, i) | ||
| }); | ||
| */ | ||
| /* | ||
| getTemperatureReport([ | ||
| "London", | ||
| "Paris", | ||
| "São Paulo" | ||
| ]) | ||
| */ | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
|
|
@@ -31,6 +51,7 @@ function temperatureService(city) { | |
|
|
||
| return temparatureMap.get(city); | ||
| } | ||
| // getTemperatureReport(cities); | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
| let usersCities = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,22 @@ | |
| // 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 | ||
|
|
||
| let i = 0; | ||
| do { | ||
| i = generateRandomNumber(); | ||
|
|
||
|
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. Tests pass, good work 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. Tests pass, good work |
||
| } while (i < 50); | ||
|
|
||
| return i; | ||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("Returned value should always be greater than 50", () => { | ||
|
|
||
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.
Very good! All your answers here are fairly clear and detailed, they show a good understanding level