-
-
Notifications
You must be signed in to change notification settings - Fork 261
LONDON_10 || SAIM KORKMAZ || JS1-4 #230
base: main
Are you sure you want to change the base?
Changes from all commits
91e96a3
bbcbc65
ef206c5
333a64a
bdfa651
bc3279a
0deed41
6f2b4a9
54abcdd
a0af0a4
0990179
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,15 +3,17 @@ Write a function that: | |||||
- Accepts an array as a parameter. | ||||||
- Returns a new array containing the first five elements of the passed array. | ||||||
*/ | ||||||
function first5() { | ||||||
function first5(arr) { | ||||||
return arr.filter((element, index) => index < 5); | ||||||
} | ||||||
|
||||||
/* | ||||||
Write a function that: | ||||||
- Accepts an array as a parameter. | ||||||
- Returns a new array containing the same elements, except sorted. | ||||||
*/ | ||||||
function sortArray() { | ||||||
function sortArray(arr) { | ||||||
return arr.sort(); | ||||||
} | ||||||
Comment on lines
+15
to
17
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. 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 |
||||||
|
||||||
/* | ||||||
|
@@ -24,7 +26,8 @@ Write a function that: | |||||
- Removes any forward slashes (/) in the strings. | ||||||
- Makes the strings all lowercase. | ||||||
*/ | ||||||
function tidyUpString() { | ||||||
function tidyUpString(arr) { | ||||||
return arr.map((str) => str.trim().replace(/\//g, "").toLowerCase()); | ||||||
} | ||||||
|
||||||
/* | ||||||
|
@@ -33,7 +36,9 @@ Write a function that: | |||||
- Returns a new array containing the same elements, but without the element at the passed index. | ||||||
*/ | ||||||
|
||||||
function remove() { | ||||||
function remove(arr, index) { | ||||||
const newArr = arr.filter((v, i) => index == 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. Similar comment as before, we should not use filter if we know which element we don't need:
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 |
||||||
return newArr; | ||||||
} | ||||||
|
||||||
/* | ||||||
|
@@ -44,7 +49,14 @@ Write a function that: | |||||
- Numbers greater 100 must be replaced with 100. | ||||||
*/ | ||||||
|
||||||
function formatPercentage() { | ||||||
function formatPercentage(arr) { | ||||||
const formattedPercentages = numbers.map((number) => { | ||||||
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.
Suggested change
numbers is not defined here and also your tests are not passing for this function. I'd suggest using |
||||||
if (number > 100) { | ||||||
number = 100; | ||||||
} | ||||||
return (Math.round(number * 100) / 100).toFixed(2) + "%"; | ||||||
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.
Suggested change
toFixed is not needed here, it will already round to 2 decimal places. |
||||||
}); | ||||||
return formattedPercentages; | ||||||
} | ||||||
|
||||||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,11 +23,19 @@ | |||||||||||||||||||
|
||||||||||||||||||||
function isBushSafe(berryArray) { | ||||||||||||||||||||
//Write your code here | ||||||||||||||||||||
} | ||||||||||||||||||||
if (berryArray.every(v => v == "pink")) { | ||||||||||||||||||||
return "Bush is safe to eat from"; | ||||||||||||||||||||
} else { | ||||||||||||||||||||
return "Toxic! Leave bush alone!"; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+26
to
+30
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.
Suggested change
We can skip the else statement for a cleaner code |
||||||||||||||||||||
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
console.log(isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"])); | ||||||||||||||||||||
|
||||||||||||||||||||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||||||||||||||||||||
|
||||||||||||||||||||
test("isBushSafe finds toxic busy", () => { | ||||||||||||||||||||
test("isBushSafe finds toxic busy", () => { | ||||||||||||||||||||
expect( | ||||||||||||||||||||
isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"]) | ||||||||||||||||||||
).toEqual("Toxic! Leave bush alone!"); | ||||||||||||||||||||
|
@@ -37,4 +45,4 @@ test("isBushSafe function finds safe bush", () => { | |||||||||||||||||||
expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual( | ||||||||||||||||||||
"Bush is safe to eat from" | ||||||||||||||||||||
); | ||||||||||||||||||||
}); | ||||||||||||||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,11 @@ | |
|
||
*/ | ||
|
||
function getSettlers() {} | ||
function getSettlers(arr) { | ||
return (result = arr.filter( | ||
(v) => v.startsWith("A") && v.includes("family") | ||
)); | ||
Comment on lines
+19
to
+21
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. Can you explain why you need result here? |
||
} | ||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,17 @@ | |
- Returns an array containing only the names of the who have attended AT LEAST 8 classes | ||
*/ | ||
|
||
function getEligibleStudents() {} | ||
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 | ||
} | ||
Comment on lines
+10
to
+20
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 works fine, but since this week was focused on array methods - it would be nice if you can rewrite with |
||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ | |
function checkCodeIsThere(stringText) { | ||
let magicWord = "code"; | ||
//edit code below | ||
if (stringText) { | ||
return stringText; | ||
if (stringText.includes(magicWord)) { | ||
return stringText.indexOf(magicWord); | ||
} else { | ||
return "Not found"; | ||
} | ||
|
@@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) { | |
|
||
Hint: Use the corresponding array method to split the array. | ||
*/ | ||
function getTransportModes() {} | ||
function getTransportModes(arr) { | ||
return arr.splice(1, arr.length) | ||
} | ||
|
||
/* | ||
Implement the function isAccessibleByTransportMode that | ||
|
@@ -81,7 +83,9 @@ function getTransportModes() {} | |
|
||
Hint: Use the corresponding array method to decide if an element is included in an array. | ||
*/ | ||
function isAccessibleByTransportMode() {} | ||
function isAccessibleByTransportMode(arr, str) { | ||
return arr.includes(str) | ||
} | ||
|
||
/* | ||
Implement the function getLocationName that | ||
|
@@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {} | |
- Returns the name of the location | ||
e.g: "Tower Bridge" | ||
*/ | ||
function getLocationName() {} | ||
function getLocationName(arr) { | ||
return arr[0] | ||
} | ||
|
||
/* | ||
We arrived at the final method. it won't take long if you use the previously implemented functions wisely. | ||
|
@@ -123,6 +129,13 @@ function getLocationName() {} | |
*/ | ||
function journeyPlanner(locations, transportMode) { | ||
// Implement the function body | ||
let result = []; | ||
for (let i = 0; i < locations.length; i++) { | ||
if (locations[i].includes(transportMode)){ | ||
result.push(locations[i][0]) | ||
} | ||
} | ||
return result | ||
Comment on lines
+132
to
+138
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 think the idea here was to use the functions you wrote earlier ( also, you can use |
||
} | ||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
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.
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 inn
operations.You can get the first 5 elements with 1 operation -
arr.slice(0, 5);