-
-
Notifications
You must be signed in to change notification settings - Fork 279
NW Class 5 - Mohammed Alhallaq- Javascript - Core1-Week3 #138
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 |
|---|---|---|
|
|
@@ -7,9 +7,17 @@ | |
| */ | ||
|
|
||
| function evenNumbersSum(n) { | ||
| // TODO | ||
| // TODO | ||
| let count = 0; | ||
| let sum = 0; | ||
| do { | ||
| if (!(count % 2)) sum = sum + count; | ||
|
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. Modulus. Nice 😁 |
||
| count+=2; | ||
| } while (count < n * 2); | ||
|
|
||
| return sum; | ||
| } | ||
|
|
||
| console.log(evenNumbersSum(3)); // should output 6 | ||
| console.log(evenNumbersSum(0)); // should output 0 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| console.log(evenNumbersSum(10)); // should output 90 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,53 +11,83 @@ | |
| - Hint: you can call the temperatureService function from your function | ||
| */ | ||
|
|
||
| function getTemperatureReport(cities) { | ||
| // TODO | ||
| let cities = [ | ||
| "London", | ||
| "Paris", | ||
| "Barcelona", | ||
| "Dubai", | ||
| "Mumbai", | ||
| "São Paulo", | ||
| "Lagos", | ||
| ]; | ||
| let citiesTemp = [ | ||
| "London", | ||
| 10, | ||
| "Paris", | ||
| 12, | ||
| "Barcelona", | ||
| 17, | ||
| "Dubai", | ||
| 27, | ||
| "Mumbai", | ||
| 29, | ||
| "São Paulo", | ||
| 23, | ||
| "Lagos", | ||
| 33, | ||
| ]; | ||
|
|
||
| function getTempratur(city) { | ||
| let index = citiesTemp.indexOf(city); | ||
| index++; | ||
| return citiesTemp[index]; | ||
| } | ||
|
Comment on lines
+14
to
44
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 an interesting approach! I think in some lower-level programming languages, storing info like this (an array with alternating keys and values) might be more common place. In JS, it's more conventional to use a dictionary: const cityTemperatures = {
"São Paulo": 23,
"Lagos": 33
}or a |
||
|
|
||
| function getTemperatureReport(cities) { | ||
| let tempratureReport = []; | ||
| // TODO | ||
| for (let i = 0; i < cities.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. Because you don't need the index (
Author
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. I agree, thank you. |
||
| let temp = getTempratur(cities[i]); | ||
| tempratureReport[i] = `The temperature in ${cities[i]} is ${temp} degrees`; | ||
| } | ||
| return tempratureReport; | ||
| } | ||
|
|
||
| /* ======= 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); | ||
| return temparatureMap.get(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); | ||
|
|
||
| return temparatureMap.get(city); | ||
| } | ||
|
|
||
| test("should return a temperature report for the user's cities", () => { | ||
| let usersCities = [ | ||
| "London", | ||
| "Paris", | ||
| "São Paulo" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees" | ||
| ]); | ||
| let usersCities = ["London", "Paris", "São Paulo"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in London is 10 degrees", | ||
| "The temperature in Paris is 12 degrees", | ||
| "The temperature in São Paulo is 23 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return a temperature report for the user's cities (alternate input)", () => { | ||
| let usersCities = [ | ||
| "Barcelona", | ||
| "Dubai" | ||
| ] | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees" | ||
| ]); | ||
| let usersCities = ["Barcelona", "Dubai"]; | ||
|
|
||
| expect(getTemperatureReport(usersCities)).toEqual([ | ||
| "The temperature in Barcelona is 17 degrees", | ||
| "The temperature in Dubai is 27 degrees", | ||
| ]); | ||
| }); | ||
|
|
||
| test("should return an empty array if the user hasn't selected any cities", () => { | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| expect(getTemperatureReport([])).toEqual([]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,20 +5,25 @@ | |
|
|
||
| // This function shouldn't be changed | ||
| function generateRandomNumber() { | ||
| console.log("Generating number..."); | ||
| return Math.round(Math.random() * 100); | ||
| console.log("Generating number..."); | ||
| return Math.round(Math.random() * 100); | ||
| } | ||
|
|
||
| function getRandomNumberGreaterThan50() { | ||
| // TODO - implement using a do-while loop | ||
| // TODO - implement using a do-while loop | ||
| let randomNumber; | ||
|
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. Nice. Simple and concise 👌 |
||
| do { | ||
| randomNumber = generateRandomNumber(); | ||
| } while (randomNumber <= 50); | ||
| return randomNumber; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
|
||
| test("Returned value should always be greater than 50", () => { | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); | ||
| expect(getRandomNumberGreaterThan50()).toBeGreaterThan(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.
Actually, it's
undefined, notnull. For something to benullwe have to explicitly mark it as such, like this:You would use
nullwhen you want to intentionally mark something as empty/missing, e.g. in this list, the singer Cher has no last name - this isn't a mistake, it's just a fact of her name: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.
Thanks Tom for this clear elaborated explaination. :-)