Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London10-Onur-Atas-JavaScript-Core-1-Coursework-Week4 #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ 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(array) {
return array.slice(0,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(array) {
array.sort();
}

/*
Expand All @@ -24,7 +27,9 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(array) {
return array.map((array => array.trim().replace (/, ' ')));

}

/*
Expand All @@ -33,9 +38,14 @@ Write a function that:
- Returns a new array containing the same elements, but without the element at the passed index.
*/

function remove() {
function remove(array , index) {
return array.filter(function(element, idx) {
return idx !== index;
});
}



/*
Write a function that:
- Takes an array of numbers as input.
Expand All @@ -44,9 +54,18 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(numbers) {
return numbers.map(function(number) {

var roundedNumber = Math.round(number * 100) / 100;
var cappedNumber = Math.min(roundedNumber, 100);
var formattedPercentage = cappedNumber.toFixed(2) + "%";

return formattedPercentage;
});
}


/* ======= TESTS - DO NOT MODIFY ===== */

test("first5 function works for more than five elements", () => {
Expand Down
15 changes: 14 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(planets) {

for (let i = 0; i < planets.length; i++) {

const planet = planets[i];
const oxygenLevel = Number(planet.substring(0, planet.length - 1));
if (oxygenLevel >= 19.5 && oxygenLevel <= 23.5) {
return planet;
}
}
return "No safe oxygen level found.";
}



/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
9 changes: 8 additions & 1 deletion 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
*/

function isBushSafe(berryArray) {
//Write your code here
const hasNonPinkBerries = berryArray.some(berry => berry !== 'pink');
const hasOnlyPinkBerries = !hasNonPinkBerries && berryArray.length > 0;

if (hasOnlyPinkBerries) {
return "bush is safe to eat from.";
} else {
return "Toxic! Leave bush alone!";
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
7 changes: 6 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

*/

function getSettlers() {}
function getSettlers(colonisers)
{ const settlers = colonisers.filter(coloniser => {
return coloniser.startsWith('A') && coloniser.endsWith('family');
});

return settlers;}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
9 changes: 8 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}
function getEligibleStudents() {
const eligibleStudents = students
.filter(student => student.attendance >= 8)
.map(student => student.name);

return eligibleStudents;

}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
5 changes: 4 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
HINT: string and array methods that could be helpful (indexOf, filter)
*/

function getLanes() {}
function getLanes(streetNames) {
return streetNames.filter(name => name.includes('Lane'));
}


/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down