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

Glasgow-6_JS1-Week-4_Ehdaa-Sakawi #237

Open
wants to merge 4 commits 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
4 changes: 3 additions & 1 deletion .gitignore
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
39 changes: 34 additions & 5 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +8 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

}


}

/*
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

Choose a reason for hiding this comment

The 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
function sortArray(letters) {
let copyOfOriginal = [...letters];
copyOfOriginal.sort();
return copyOfOriginal;
function sortArray(letters) {
let copyOfOriginal = [...letters];
return copyOfOriginal.sort();
}

}

/*
Expand All @@ -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("/", ""));

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way of doing it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way of doing it 👏

}

/*
Expand All @@ -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

Choose a reason for hiding this comment

The 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.
numbers.map(addPercentage);

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 ===== */
Expand Down
11 changes: 10 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "%", " . ";
}

Choose a reason for hiding this comment

The 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. parseFloat will turn a percentage into a number, e.g. parseFloat("25%") === 25

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 ===== */

Expand Down
7 changes: 7 additions & 0 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

}


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

test("isBushSafe finds toxic busy", () => {
Expand Down
11 changes: 10 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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", () => {
Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👏


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

test("getEligibleStudents function works", () => {
Expand Down
18 changes: 13 additions & 5 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
if (stringText.text.includes(magicWord)) {
return stringText.indexOf(magicWord);;
} else {
return "Not found";
}
Expand Down Expand Up @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}
function getTransportModes(location) {
return location.slice(1);
}

/*
Implement the function isAccessibleByTransportMode that
Expand All @@ -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(transportMode, transport) {
return transportMode.includes(transport);
}

/*
Implement the function getLocationName that
Expand All @@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {}
- Returns the name of the location
e.g: "Tower Bridge"
*/
function getLocationName() {}
function getLocationName(Array) {
return Array[0];
}

/*
We arrived at the final method. it won't take long if you use the previously implemented functions wisely.
Expand Down Expand Up @@ -122,6 +128,8 @@ function getLocationName() {}
Advanced challange: try to use arrow function when invoking an array method.
*/
function journeyPlanner(locations, transportMode) {
return locations.filter((Array) => Arrayincludes(transportMode))
.map((location) => Array[0])
// Implement the function body
}

Expand Down
4 changes: 3 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


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

Expand Down
34 changes: 32 additions & 2 deletions 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,45 @@ PasswordValidationResult= [false, false, false, false, true]

*/

function validatePasswords(passwords) {}

const arr = ["fhD8!yrjj", "ttkT", "dvyy", "ttkT", "qwbfj76%", "tytT3729."];

function validatePasswords(passwords) {
const result = [];
for (let i = 0; i < passwords.length; i++) {
const check1 = containsAtLeast5Chars(passwords[i]);
const check2 = containsUppercaseLetter(passwords[i]);
const check3 = containsLowercaseLetter(passwords[i]);
const check4 = containsNumber(passwords[i]);
const check5 = containsSymbol(passwords[i]);
let isNotRepeatedPassword = true;
for (let j = 0; j < i; j++) {
if (passwords[j] === passwords[i]) {
isNotRepeatedPassword = false;
}
}
result.push(
check1 && check2 && check3 && check4 && check5 && isNotRepeatedPassword
);
}
return result;
}




// Returns true if string has at least 5 characters
function containsAtLeast5Chars(string) {
return string.length >= 5;
}

// Returns true if string contains at least one uppercase letter.
function containsUppercaseLetter(string) {
return /[A-Z]/.test(string);
}

// Returns true if string contains at least one lowercase letter.
function containsLowercaseLetter(string) {

return /[a-z]/.test(string);
}

Expand Down