-
-
Notifications
You must be signed in to change notification settings - Fork 261
Glasgow-6_JS1-Week-4_Ehdaa-Sakawi #237
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,4 +1,6 @@ | ||
node_modules | ||
.DS_Store | ||
|
||
/package-lock.json | ||
/package-lock.json | ||
practice.js | ||
*.js |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,15 +3,24 @@ 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) { | ||||||||||||||||||
//let numbers = [ 1, 2, 3, 4, 5, 6, 7] | ||||||||||||||||||
let copyOfOriginal = Arr.slice(0, 5); { | ||||||||||||||||||
return copyOfOriginal; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/* | ||||||||||||||||||
Write a function that: | ||||||||||||||||||
- Accepts an array as a parameter. | ||||||||||||||||||
- Returns a new array containing the same elements, except sorted. | ||||||||||||||||||
*/ | ||||||||||||||||||
function sortArray() { | ||||||||||||||||||
function sortArray(letters) { | ||||||||||||||||||
let copyOfOriginal = [...letters]; | ||||||||||||||||||
copyOfOriginal.sort(); | ||||||||||||||||||
return copyOfOriginal; | ||||||||||||||||||
Comment on lines
+20
to
+23
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 good. We could also have just directly returned the sorted version like this
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/* | ||||||||||||||||||
|
@@ -24,16 +33,26 @@ Write a function that: | |||||||||||||||||
- Removes any forward slashes (/) in the strings. | ||||||||||||||||||
- Makes the strings all lowercase. | ||||||||||||||||||
*/ | ||||||||||||||||||
function tidyUpString() { | ||||||||||||||||||
function tidyUpString(array) { | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
return array.map((string) => string.trim().toLowerCase().replace("/", "")); | ||||||||||||||||||
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 use of method chaining! 👏 |
||||||||||||||||||
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
/* | ||||||||||||||||||
Write a function that: | ||||||||||||||||||
- Takes an array and an index as input. | ||||||||||||||||||
- Returns a new array containing the same elements, but without the element at the passed index. | ||||||||||||||||||
*/ | ||||||||||||||||||
// | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
function remove() { | ||||||||||||||||||
function remove(array, index) { | ||||||||||||||||||
|
||||||||||||||||||
return array.slice(0, index).concat(array.slice(index + 1)); | ||||||||||||||||||
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 way of doing it 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 way of doing it 👏 |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/* | ||||||||||||||||||
|
@@ -44,7 +63,17 @@ Write a function that: | |||||||||||||||||
- Numbers greater 100 must be replaced with 100. | ||||||||||||||||||
*/ | ||||||||||||||||||
|
||||||||||||||||||
function formatPercentage() { | ||||||||||||||||||
function formatPercentage(numbers) { | ||||||||||||||||||
numbersWithPercentages = numbers.map(addPercentage); | ||||||||||||||||||
return numbersWithPercentages; | ||||||||||||||||||
Comment on lines
+67
to
+68
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. Here we could simply return the map directly without saving it in a variable and then returning that. Good use of map though 👏 |
||||||||||||||||||
|
||||||||||||||||||
} | ||||||||||||||||||
function addPercentage(number) { | ||||||||||||||||||
if (number > 100) { | ||||||||||||||||||
number = 100; | ||||||||||||||||||
} | ||||||||||||||||||
number = Math.round(number * 100) / 100; | ||||||||||||||||||
return `${number}%`; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,17 @@ | |
|
||
Some string methods that might help you here are .replace() and .substring(). | ||
*/ | ||
let oxegenLevel= []; | ||
function findSafeOxygenLevel(result) { | ||
if(value > 19.5 && value < 23.5) | ||
return result === "%", " . "; | ||
} | ||
|
||
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. Here the function should take an array of percentages as an input (see tests for examples below). So first we need to turn the percentage which is currently a strings into a number. We need to find the first number in the array that is between 19.5 and 23.5. function findSafeOxygenLevel(planetPercentages) {
return planetPercentages.find(function(percentage) {
const number = percentage.parseFloat(percentage)
return (number >= 19.5 && number <= 23.5) // This will return true or false, if it returns true, the `find` array function will return it as the element it finds first
})
} |
||
function findSafeOxygenLevel() {} | ||
//let array=[value]; | ||
//return array.find((value) => value > 19.5 , "%" && value < 23.5 , "%"); | ||
|
||
|
||
|
||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,15 @@ | |
|
||
function isBushSafe(berryArray) { | ||
//Write your code here | ||
|
||
if (berryArray.every((berry) => berry === "pink")) { | ||
return "Bush is safe to eat from"; | ||
} else { | ||
return "Toxic! Leave bush alone!"; | ||
} | ||
Comment on lines
+26
to
+31
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. Great! |
||
} | ||
|
||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
test("isBushSafe finds toxic busy", () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,17 @@ | |
|
||
*/ | ||
|
||
function getSettlers() {} | ||
function getSettlers(voyagers) { | ||
|
||
let array=[]; | ||
let array1= [ array+"family" , "family"]; | ||
for(let familyName of voyagers){ | ||
if(familyName.split(' ').slice(-1).join(' ') && familyName[0] === "A"){ | ||
array.push(familyName)} | ||
} | ||
|
||
return array1; | ||
Comment on lines
+20
to
+27
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. You could use filter here. (Note: I have not run this code so haven't checked syntax) function getSettlers(voyagers) {
return voyagers.filter(function(voyager) {
// separate the name into an array of strings
const names = voyager.split(' ');
// check the name includes the string "family", the first word starts with "a"
if (names.includes("family") && names[0].startsWith("a")) {
return true
}
return false
})
} |
||
} | ||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
test("getSettlers function works", () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,12 @@ | |
- Returns an array containing only the names of the who have attended AT LEAST 8 classes | ||
*/ | ||
|
||
function getEligibleStudents() {} | ||
function getEligibleStudents(attended) { | ||
|
||
const eligibleAttendance = attended.filter(attendance => attendance[1] >= 8); | ||
return eligibleAttendance.map(names=> names[0]) | ||
Comment on lines
+12
to
+13
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. Great 👏 |
||
|
||
} | ||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
test("getEligibleStudents function works", () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
HINT: string and array methods that could be helpful (indexOf, filter) | ||
*/ | ||
|
||
function getLanes() {} | ||
function getLanes(streetNames) { | ||
return streetNames.filter((street) => street.includes("Lane")); | ||
} | ||
Comment on lines
+9
to
+11
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 - 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.
👏