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

Js/ week3/ali haider #97

Open
wants to merge 15 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
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\week-3\\1-exercises\\D-array-filter\\exercise.js"
}
]
}
6 changes: 5 additions & 1 deletion week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
*/

// write your code here
function findLongNameThatStartsWithA(name) {
return name.length > 7 && name[0] === "A";
}

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

var longNameThatStartsWithA = findLongNameThatStartsWithA(names);
var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA);


console.log(longNameThatStartsWithA);

Expand Down
9 changes: 8 additions & 1 deletion week-3/1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ 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 (number){
return number === null;
}
var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

var pairs = pairsByIndex.map(function(indexes) {
if ( pairsByIndex.some(isNull) === true){
process.exit(1);
}
else{
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
}
});

console.log(pairs);
5 changes: 2 additions & 3 deletions week-3/1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

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");
} else {
Expand Down
20 changes: 13 additions & 7 deletions week-3/1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@

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

var pairsByIndex; // Complete this statement
var pairsByIndex = pairsByIndexRaw.filter(function name(arr) {
for (const key in arr) {
if (typeof key !== null && key >=1 ) {
return arr[key];
}
}

}); // Complete this statement

var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

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

var pairs = pairsByIndex.map(function (indexes) {
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
});
console.log(pairs);
5 changes: 5 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,8 @@

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

var numberMultiplied = numbers.map(function(number){
return number*100;
});

console.log(numberMultiplied);
16 changes: 16 additions & 0 deletions week-3/1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@
*/

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

var names = ["Daniel", "mozafar", "irina"];
arr.forEach(function (name, index) {
if (name % 3 === 0 && name % 5 === 0) {
console.log(name + ": " + "FizzBuzz");
}
else if (name % 5 === 0) {
console.log(name + ": " + "Buzz");
}
else if (name % 3 === 0) {
console.log(name + ": " + "Fizz");
}
else {
console.log(name + ": " + name);
}

});
/* 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
4 changes: 2 additions & 2 deletions week-3/1-exercises/H-array-methods-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var everyone = [
"Swathi"
];

var firstFive; // complete this statement
var lastFive; // complete this statement
var firstFive = everyone.slice(0,5); // complete this statement
var lastFive=everyone.slice(everyone.length-5,everyone.length); // complete this statement

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

function capitalise(str) {}
function capitalise(str) {
let newStr= str.slice(0,1).toUpperCase()+str.slice(1,str.length);

return newStr;
}

/*
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
12 changes: 11 additions & 1 deletion week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ 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) {
var safeLevel = arr.find(function (value){
if( parseFloat(value) > 19.5 && parseFloat(value) < 23.5){
return true;
}
else {
return false;
}
})
return safeLevel;

}


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

const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"];
Expand Down
20 changes: 18 additions & 2 deletions week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function colorCheck(color){
if (color ==="pink"){
return true;
}
}

function bushChecker(arr) {
let safeBerries = arr.every(colorCheck);
if (safeBerries === true){
return "Bush is safe to eat from";
}
else {
return "Toxic! Leave bush alone!";
}
}





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

let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"]
Expand Down
5 changes: 4 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,10 @@
NOTE: don't include any element that is not a "family".
*/

function colonisers() {
function colonisers(arr) {
return arr.filter(function(name){
return name.startsWith("A") && name.includes("family");
})

}

Expand Down
11 changes: 9 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,14 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents() {

function eligibleStudents(arr) {
let nameArr = [];
for(let i=0;i<arr.length;i++){
if(arr[i][1]>=8){
nameArr.push(arr[i][0]);
}
}
return nameArr;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -22,6 +28,7 @@ const attendances = [
["Nina", 10]
]


const util = require('util');

function test(test_name, actual, expected) {
Expand Down
11 changes: 10 additions & 1 deletion week-3/2-mandatory/5-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
NOTE: only the names should be returned, not the means of transport.
*/

function journeyPlanner() {
function journeyPlanner(arr,mode) {
let nameArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].includes(mode)) {
nameArr.push(arr[i][0]);
}
}
return nameArr;
}



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

const londonLocations = [
Expand Down
12 changes: 10 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,18 @@
Write a function that will return all street names which contain 'Lane' in their name.
*/

function getLanes() {

function getLanes(arr,name) {
let nameArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].includes("Lane")) {
nameArr.push(arr[i]);
}
}
return nameArr;
}



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

const streetNames = [
Expand Down
55 changes: 43 additions & 12 deletions week-3/2-mandatory/7-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,64 @@ PasswordValidationResult= [false, false, false, false, true]

*/

function validatePasswords(passwords) {
function validatePasswords(arr) {



var passw = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z]).{5,}$/;
let nameArr = [];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j <= arr.length; j++) {
if (arr[i] === arr[j]) {
arr[j] = nameArr.push(false);
nameArr.shift();
}
}
}

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

if (passw.test(arr[i])){
nameArr.push(true);

}

else {
nameArr.push(false);
}

}
return nameArr;


}


/* ======= 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');
const util = require("util");

function test(test_name, actual, expected) {
let status;
if (util.isDeepStrictEqual(actual, expected)) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
}
let status;
if (util.isDeepStrictEqual(actual, expected)) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(
expected
)} but your function returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
console.log(`${test_name}: ${status}`);
}

test(
"validatePasswords function works - case 1",
validatePasswords(passwords1),
[false, false, true, false, false]
);
);

test(
"validatePasswords function works - case 2",
Expand Down