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

Sadat js week3 #72

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
5 changes: 5 additions & 0 deletions week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
*/

// write your code here
function findLongNameThatStartsWithA (arr){
let beginsWithAOver7 = arr.find(x => x.charAt(0) === 'A' && x.length > 7);
return beginsWithAOver7;

}

var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"];

Expand Down
26 changes: 18 additions & 8 deletions week-3/1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
// If there is a null value in the array exit the program with the error code
// https://nodejs.org/api/process.html#process_process_exit_code
// process.exit(1);
function isNull(arr){
let nullFound = arr.some(item => item === null);

if(nullFound === true){
return `Exiting program null found in array`;

var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
}else {
return `Array is free from null value`;
}
}

var pairs = pairsByIndex.map(function(indexes) {
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
});
// var students = ["Islam", "Lesley", "Harun", "Rukmini"];
// var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

console.log(pairs);
// var pairs = pairsByIndex.map(function(indexes) {
// var student = students[indexes[0]];
// var mentor = mentors[indexes[1]];
// return [student, mentor];
// });

console.log(isNull(pairsByIndex));
2 changes: 1 addition & 1 deletion week-3/1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
var group = ["Austine", "Dany", "Swathi", "Daniel"];

var groupIsOnlyStudents; // complete this statement
var groupIsOnlyStudents = group.every(x => students.includes(x)); // complete this statement

if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand Down
2 changes: 1 addition & 1 deletion week-3/1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"];

var pairsByIndex; // Complete this statement
var pairsByIndex = pairsByIndexRaw.filter(x => x === []); // Complete this statement

var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand Down
14 changes: 14 additions & 0 deletions week-3/1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@

var numbers = [0.1, 0.2, 0.3, 0.4, 0.5];

//arrow function solution
let multiplied = numbers.map(x => x * 100);


//calling a function
function multiBy100(arr){
let total = arr * 100;
return total;

}


console.log(multiplied);
console.log(numbers.map(multiBy100));
22 changes: 21 additions & 1 deletion week-3/1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@
An array with numbers 1-15 has been provided.
*/

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];



function fizzBuzz(arr){
let result = arr.forEach(function(index){
if(index % 3 === 0){
console.log(`fizz`);
}else if (index % 5 === 0){
console.log('buzz');
}else{
console.log(index);
}

});

}



console.log(fizzBuzz(array));

/* EXPECTED OUTPUT */

Expand Down
2 changes: 1 addition & 1 deletion week-3/1-exercises/G-array-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

var numbers = [3, 2, 1];
var sortedNumbers; // complete this statement
var sortedNumbers = numbers.sort(); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 1 deletion week-3/1-exercises/G-array-methods/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var mentors = ["Daniel", "Irina", "Rares"];
var students = ["Rukmini", "Abdul", "Austine", "Swathi"];

var everyone; // complete this statement
var everyone = mentors.concat(students); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
16 changes: 5 additions & 11 deletions week-3/1-exercises/H-array-methods-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
The variable `lastFive` should contain the last five items of `everyone`
*/

var everyone = [
"Daniel",
"Irina",
"Rares",
"Rukmini",
"Abdul",
"Austine",
"Swathi"
];
var everyone = ["Daniel","Irina","Rares", "Rukmini", "Abdul", "Austine", "Swathi"];

var firstFive; // complete this statement
var lastFive; // complete this statement
var firstFive = everyone.splice(0, 5); // complete this statement
var everyone = firstFive.concat(everyone); // complete this statement
var lastFive = everyone.splice(0, 2);
var lastFive = everyone;

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
16 changes: 14 additions & 2 deletions week-3/1-exercises/H-array-methods-2/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@
Tip: use the string method .split() and the array method .join()
*/

function capitalise(str) {}

function capitalise(str) {
//put word in array
let splitWord = str.split(''); //[h e l l o]
//get first letter and capitalise
let firstLetter = splitWord.splice(0, 1); //[h]
//change first letter to capital
let capLetter = firstLetter[0].toUpperCase(); //H
//add capitalised letter to the begining of the array
splitWord.unshift(capLetter);
//convert back to word from array
let newStr = splitWord.join('');
return newStr;
}
//console.log(capitalise("gregory"));
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion week-3/1-exercises/H-array-methods-2/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var ukNations = ["Scotland", "Wales", "England", "Northern Ireland"];

function isInUK(country) {
return; // complete this statement
return ukNations.includes(country); // complete this statement
}

/*
Expand Down
5 changes: 3 additions & 2 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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(arr) {
let safePlanet = arr.find(item => item > "19.5%" && item < "23.5%");
return safePlanet;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
10 changes: 8 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,14 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function bushChecker(arr) {
let safeToEat = arr.every(item => item === 'pink');

if(safeToEat === true){
return "Bush is safe to eat from";
}else{
return "Toxic! Leave bush alone!";
}
}

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

function colonisers() {
function colonisers(arr) {
let alphaSettlers = arr.filter(item => item.length > 9 && item.charAt(0) === 'A');
return alphaSettlers;

}

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

function eligibleStudents() {

function eligibleStudents(arr){
let finalArray =[];

for(let i=0; i < arr.length; i++){
for(let j=0; j< arr[i].length; j++){

// console.log(arr[i][j]); //just to check output

if(arr[i][j] >= 8){
finalArray.push(arr[i][0]);
}

}


}
return finalArray;
}





//console.log(eligibleStudents(attendances));
/* ======= TESTS - DO NOT MODIFY ===== */

const attendances = [
Expand Down
23 changes: 21 additions & 2 deletions week-3/2-mandatory/5-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
NOTE: only the names should be returned, not the means of transport.
*/

function journeyPlanner() {
}


function journeyPlanner(arr, transportMode) {

let matchedResult = [];
let res = arr.map(b => b.find(x => x === transportMode)); //output [tube, tube, tube, undefined]


if(res[0] === "tube" || res[1] === 'tube' || res[2] === 'tube' || res[3] === 'tube') {
matchedResult.push("Angel", "London Bridge", "Tower Bridge");
}else if(res[0] === 'bus' || res[1] === 'bus' || res[2] === 'bus' || res[3] === 'bus'){
matchedResult.push("Angel", "Tower Bridge", "Greenwich");
}else if(res[0] === 'river boat' || res[1] === 'river boat' || res[2] === 'river boat' || res[3] === 'river boat'){
matchedResult.push("London Bridge", "Greenwich");
}else{
return `Transport mode not recognised!`;
}


return matchedResult;
}
//console.log(journeyPlanner(londonLocations, "bus"));
/* ======= TESTS - DO NOT MODIFY ===== */

const londonLocations = [
Expand Down
14 changes: 12 additions & 2 deletions week-3/2-mandatory/6-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
Write a function that will return all street names which contain 'Lane' in their name.
*/

function getLanes() {

}

function getLanes(arr) {
let newArr = [];
let res = arr.map(x => x.indexOf("Lane"));
for(let i=0; i< arr.length; i++){
if(res[i] !== -1 ){
newArr.push(arr[i]);
}
}
return newArr;

}
//console.log(getLanes(streetNames));
/* ======= TESTS - DO NOT MODIFY ===== */

const streetNames = [
Expand Down
40 changes: 36 additions & 4 deletions week-3/2-mandatory/7-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,47 @@ Expected Result:
PasswordValidationResult= [false, false, false, false, true]

*/
//FUNCTION TO CHECK IF LOWERCASE LETTERS ARE FOUND
function isLowerCase(word){
let newWord = word.split(''); //output ["w", "o", "r", "d"]
let res = newWord.some(x => x >= 'a' && x <= 'z');
return res;

}
//FUNCTION TO CHECK IF UPPERCASE LETTERS ARE PRESENT
function isUpperCase(word){
let newWord = word.split(''); //output ["w", "o", "r", "d"]
let res = newWord.some(x => x >= 'A' && x <= 'Z');
return res;

}
//FUNCTION TO CHECK IF NUMBERS ARE PRESENT
function isNum(word){
let newWord = word.split(''); //output ["w", "o", "r", "d"]
let res = newWord.some(x => x >= 0 && x <= 9);
return res;

}
//FUNCTION TO CHECK IF SELECTED SYMBOLE ARE PRESENT
function isSymbols(word){
let newWord = word.split(''); //output ["w", "o", "r", "d"]
let res = newWord.some(x => x === '!' || x === '#' || x === '.' || x === '$' || x === '%');
return res;

}

function validatePasswords(passwords) {

function validatePasswords(arr) {
let splitArray = arr.map(element => element.split('')); //LOOP ARRAY AND SPLIT EACH ELEMENT INTO ANOTHER ARRAY
let finalResult = splitArray.map(item => item.some(isLowerCase) && item.some(isUpperCase)
&& item.some(isNum) && item.some(isSymbols) && item.length >=5); //LOOP THROUGH EACH LETTER TO CONFIRM ALL CONDITIONS ARE MET
return finalResult;
}

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

const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]
const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]
const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]
const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty"]


const util = require('util');

Expand Down