Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.

Week3/mandatory #81

Open
wants to merge 7 commits into
base: master
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
6 changes: 5 additions & 1 deletion week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ To be safe to land on, a planet needs to have an Oxygen level between 19.5% and
Write a function that finds the first safe oxygen level in the array - Oxygen between 19.5% and 23.5%
*/

function safeLevels() {
function safeLevels(levels) {
return levels.find(level => {
let levelNum = parseFloat(level)
return (levelNum > 19.5) && (levelNum < 23.5)
})

}

Expand Down
7 changes: 5 additions & 2 deletions week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function bushChecker(bush) {
if (bush.every(color => color === "pink"))
return "Bush is safe to eat from"
else
return "Toxic! Leave bush alone!"
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 2 additions & 2 deletions week-3/2-mandatory/3-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
NOTE: don't include any element that is not a "family".
*/

function colonisers() {

function colonisers(families) {
return families.filter(family => family.includes("family") && (family[0] === "A"))
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 2 additions & 2 deletions week-3/2-mandatory/4-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents() {

function eligibleStudents(students) {
return students.filter(student => student[1] >= 8).map(student => student[0])
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
3 changes: 2 additions & 1 deletion week-3/2-mandatory/5-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
NOTE: only the names should be returned, not the means of transport.
*/

function journeyPlanner() {
function journeyPlanner(locations, travelType) {
return locations.filter(location => location.includes(travelType)).map(location => location[0])
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 2 additions & 2 deletions week-3/2-mandatory/6-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Write a function that will return all street names which contain 'Lane' in their name.
*/

function getLanes() {

function getLanes(streets) {
return streets.filter(street => street.includes("Lane"))
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
60 changes: 59 additions & 1 deletion week-3/2-mandatory/7-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,66 @@ PasswordValidationResult= [false, false, false, false, true]

*/

function validatePasswords(passwords) {
const IS_UPPER = 1
const IS_LOWER = 2
const IS_NUMBER = 4
const IS_NON_APLHANUM = 8
const IS_INVALID_CHAR = 16
const VALID_PASSWD = 15

/*
* validChars(char) - takes a character as input and tests whether it is lowercase, uppercase,
* a digit (0 - 9), or a strict set of alphanumerics ("!", "#", "$", "%", ".").
* All tests performed use regular expressions
*/

function isValidChar(char) {
if (/^[a-z]/.test(char))
return IS_LOWER

if (/^[A-Z]/.test(char))
return IS_UPPER
// if (Number.isInteger(Number.parseInt(char)))
if (/^[0-9]/.test(char))
return IS_NUMBER
// if (["!", "#", "$", "%", "."].includes(char))
if (/^[!#$%.]/.test(char))
return IS_NON_APLHANUM

return IS_INVALID_CHAR
}


/*
* isValidPassword(password, index, passwdArray) -
* password - current password from array of passwords
* index - password's current array index
* passwdArray - the array of passwords
*
*
*/
function isValidPassword(password, index, passwdArray) {
let flags = 0
if (password.length >= 5) { // All valid passwords are at least 5 characters long
if (passwdArray.indexOf(password) === index) { // filter out duplicates
/*
use reduce() to store the result of accumulated bit-wise OR operations on the flags variable
validChars(char) can return any of the following:
IS_LOWER
IS_UPPER
IS_NUMBER
IS_NON_APLHANUM
IS_INVALID_CHAR
*/
return password.split("").reduce((flags, char) => flags | isValidChar(char), 0) === VALID_PASSWD
}
}

return false
}

function validatePasswords(passwords) {
return passwords.map(isValidPassword)
}

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