LONDON_10 || SAIM KORKMAZ || JS1-4#230
Conversation
used {return...} instead only statement
| */ | ||
| function first5() { | ||
| function first5(arr) { | ||
| return arr.filter((element, index) => index < 5); |
There was a problem hiding this comment.
This is not a good approach, filtering is an expensive operation that iterates over each element in the array. If array has n elements, it will result in n operations.
You can get the first 5 elements with 1 operation - arr.slice(0, 5);
|
|
||
| function remove() { | ||
| function remove(arr, index) { | ||
| const newArr = arr.filter((v, i) => index == i); |
There was a problem hiding this comment.
Similar comment as before, we should not use filter if we know which element we don't need:
const removedElement = arr.splice(index,1)
return arr // this will not contain removedElement
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
This modifies the source array, so if you want to avoid that - you can create a copy via const arr1 = [...arr];
|
|
||
| function formatPercentage() { | ||
| function formatPercentage(arr) { | ||
| const formattedPercentages = numbers.map((number) => { |
There was a problem hiding this comment.
| const formattedPercentages = numbers.map((number) => { | |
| const formattedPercentages = arr.map((number) => { |
numbers is not defined here and also your tests are not passing for this function.
I'd suggest using Math.round(num * 100) / 100 instead of toFixed()
| if (number > 100) { | ||
| number = 100; | ||
| } | ||
| return (Math.round(number * 100) / 100).toFixed(2) + "%"; |
There was a problem hiding this comment.
| return (Math.round(number * 100) / 100).toFixed(2) + "%"; | |
| return (Math.round(number * 100) / 100)+ "%"; |
toFixed is not needed here, it will already round to 2 decimal places.
| function sortArray(arr) { | ||
| return arr.sort(); | ||
| } |
There was a problem hiding this comment.
Some tests for this function are failing because it modifies the passed in array. I'd suggest making a copy of the array and sorting that, e.g [...arr].sort();
| if (berryArray.every(v => v == "pink")) { | ||
| return "Bush is safe to eat from"; | ||
| } else { | ||
| return "Toxic! Leave bush alone!"; | ||
| } |
There was a problem hiding this comment.
| if (berryArray.every(v => v == "pink")) { | |
| return "Bush is safe to eat from"; | |
| } else { | |
| return "Toxic! Leave bush alone!"; | |
| } | |
| if (berryArray.every(v => v == "pink")) { | |
| return "Bush is safe to eat from"; | |
| } | |
| return "Toxic! Leave bush alone!"; |
We can skip the else statement for a cleaner code
| return (result = arr.filter( | ||
| (v) => v.startsWith("A") && v.includes("family") | ||
| )); |
There was a problem hiding this comment.
Can you explain why you need result here?
| function getEligibleStudents(arr) { | ||
| let result = []; | ||
|
|
||
| for (let i = 0; i < arr.length; i++) { | ||
| if (arr[i][1] > 7) { | ||
| result.push(arr[i][0]) | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } |
There was a problem hiding this comment.
This works fine, but since this week was focused on array methods - it would be nice if you can rewrite with .filter() and .map()
| let result = []; | ||
| for (let i = 0; i < locations.length; i++) { | ||
| if (locations[i].includes(transportMode)){ | ||
| result.push(locations[i][0]) | ||
| } | ||
| } | ||
| return result |
There was a problem hiding this comment.
I think the idea here was to use the functions you wrote earlier (getLocationName and isAccessibleByTransportMode)
also, you can use.filter()and .map()
berkeli
left a comment
There was a problem hiding this comment.
Nice work, but I see there are multiple exercises missing in the 1-exercises folder.
Mentor: @berkeli
Mates: @mabalal , @Mr-DEM1R