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

Commit a551527

Browse files
committed
week-3-JavaScript-HomeWork(All+++)
1 parent 6975033 commit a551527

File tree

17 files changed

+169
-22
lines changed

17 files changed

+169
-22
lines changed

week-3/1-exercises/A-array-find/exercise.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters.
44
*/
55

6+
67
// write your code here
78
function findLongNameThatStartsWithA(names){
89
let output=names.find(function(name){

week-3/1-exercises/B-array-some/exercise.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
- Do not edit any of the existing code
77
*/
88

9-
var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
109

10+
var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
11+
let test=pairsByIndex.some(function(value){
12+
return value===null;
13+
})
14+
if(test){
15+
console.log("array contain null value");
16+
process.exit(1)
17+
}
18+
else{
1119
// If there is a null value in the array exit the program with the error code
1220
// https://nodejs.org/api/process.html#process_process_exit_code
1321
// process.exit(1);
1422

23+
1524
var students = ["Islam", "Lesley", "Harun", "Rukmini"];
1625
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
1726

@@ -22,3 +31,4 @@ var pairs = pairsByIndex.map(function(indexes) {
2231
});
2332

2433
console.log(pairs);
34+
}

week-3/1-exercises/C-array-every/exercise.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
66
var group = ["Austine", "Dany", "Swathi", "Daniel"];
77

8-
var groupIsOnlyStudents; // complete this statement
8+
var groupIsOnlyStudents=group.every(function(name){
9+
return students.includes(name);
10+
}); // complete this statement
911

1012
if (groupIsOnlyStudents) {
1113
console.log("The group contains only students");

week-3/1-exercises/D-array-filter/exercise.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/*
22
You are given a program that logs pairings between mentors and students
3-
It fails because the array `pairsById` can contain different values that break the program
3+
It fails because the array `pairsById` can contain different values that
4+
break the program
45
It is decided that array items which are not pairs should be filtered out
56
- Finish the statement on line 11 to produce an array with valid content
67
- Do not edit any of the existing code
78
*/
89

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

11-
var pairsByIndex; // Complete this statement
12+
var pairsByIndex=pairsByIndexRaw.filter(function(value){
13+
return Array.isArray(value) && value.length>1;
14+
}); // Complete this statement
1215

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

week-3/1-exercises/E-array-map/exercise.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22
// Write multiple solutions using different syntax (as shown in the README)
33

44
var numbers = [0.1, 0.2, 0.3, 0.4, 0.5];
5-
5+
var brandNewArray;
6+
brandNewArray=numbers.map(function(num){
7+
return num*100;
8+
})
9+
brandNewArray=numbers.map(num=>{
10+
return num*100;
11+
})
12+
brandNewArray=numbers.map(num=>num*100);
13+
console.log(brandNewArray);

week-3/1-exercises/F-array-forEach/exercise.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@
88
*/
99

1010
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
11-
11+
var output=arr.forEach(function(num,index){
12+
if(num%3===0 && num%5===0){
13+
console.log("FizzBuzz");
14+
}
15+
else if(num%5===0){
16+
console.log("Buzz");
17+
}
18+
else if(num%3===0){
19+
console.log("Fizz");
20+
}
21+
else{
22+
console.log(num);
23+
}
24+
})
25+
console.log(output);
1226
/* EXPECTED OUTPUT */
1327

1428
/*

week-3/1-exercises/G-array-methods/exercise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
var numbers = [3, 2, 1];
7-
var sortedNumbers; // complete this statement
7+
var sortedNumbers=numbers.sort(); // complete this statement
88

99
/*
1010
DO NOT EDIT BELOW THIS LINE

week-3/1-exercises/G-array-methods/exercise2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
var mentors = ["Daniel", "Irina", "Rares"];
88
var students = ["Rukmini", "Abdul", "Austine", "Swathi"];
99

10-
var everyone; // complete this statement
10+
var everyone=mentors.concat(students);// complete this statement
1111

1212
/*
1313
DO NOT EDIT BELOW THIS LINE

week-3/2-mandatory/1-oxygen-levels.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ To be safe to land on, a planet needs to have an Oxygen level between 19.5% and
99
Write a function that finds the first safe oxygen level in the array - Oxygen between 19.5% and 23.5%
1010
*/
1111

12-
function safeLevels() {
13-
12+
function safeLevels(arr) {
13+
var findLevel=arr.find(function(level){
14+
return parseFloat(level)>19.5 && parseFloat(level)<23.5;
15+
})
16+
return findLevel;
1417
}
1518

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

week-3/2-mandatory/2-bush-berries.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@
1010
Use the tests to confirm which message to return
1111
*/
1212

13-
function bushChecker() {
14-
13+
function bushChecker(arr) {
14+
if(arr.every(function(barries){
15+
return barries==="pink";
16+
})){
17+
return "Bush is safe to eat from";
18+
}
19+
else{
20+
return "Toxic! Leave bush alone!";
21+
22+
}
1523
}
1624

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

week-3/2-mandatory/3-space-colonies.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
NOTE: don't include any element that is not a "family".
99
*/
1010

11-
function colonisers() {
12-
11+
function colonisers(arr) {
12+
let output=arr.filter(function(names){
13+
return names.charAt(0)==='A' && names.includes("family");
14+
})
15+
return output;
1316
}
1417

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

week-3/2-mandatory/4-eligible-students.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
88
*/
99

10-
function eligibleStudents() {
11-
10+
function eligibleStudents(arr) {
11+
var output=arr.filter(function(record){
12+
13+
return parseFloat(record[1])>=8;
14+
}).map(function(record){
15+
return record[0];
16+
})
17+
18+
//console.log(final);
19+
return output;
1220
}
1321

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

week-3/2-mandatory/5-journey-planner.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@
77
NOTE: only the names should be returned, not the means of transport.
88
*/
99

10-
function journeyPlanner() {
10+
function journeyPlanner(arr,transport) {
11+
let output=arr.filter(function(rout){
12+
if(rout.includes(transport)){
13+
//console.log(rout[0])
14+
return rout;
15+
//return rout;
16+
};
17+
}).map(function(rout){
18+
return rout[0];
19+
})
20+
21+
22+
return output;
1123
}
1224

1325
/* ======= TESTS - DO NOT MODIFY ===== */
@@ -20,6 +32,7 @@ const londonLocations = [
2032
]
2133

2234
const util = require('util');
35+
const { runInContext } = require('vm');
2336

2437
function test(test_name, actual, expected) {
2538
let status;

week-3/2-mandatory/6-lane-names.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
Write a function that will return all street names which contain 'Lane' in their name.
55
*/
66

7-
function getLanes() {
8-
7+
function getLanes(arr) {
8+
let output=arr.filter(function(name){
9+
return name.includes("Lane");
10+
})
11+
return output;
912
}
1013

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

week-3/2-mandatory/7-password-validator.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,29 @@ PasswordValidationResult= [false, false, false, false, true]
2323
*/
2424

2525
function validatePasswords(passwords) {
26-
27-
}
26+
//exclude the duplicate value
27+
for(let i=0;i<passwords.length-1;i++){
28+
for(let j=i+1;j<=passwords.length;j++){
29+
if(passwords[i]===passwords[j]){
30+
passwords[j]="AAA";
31+
}
32+
}
33+
}
34+
//console.log("dop=",passwords);
35+
////////////////////////
36+
37+
const checkPassword = passwords.map(function(password){
38+
if(password.length>=5 && password.match(/[A-Z]/) &&
39+
password.match(/[a-z]/) && password.match(/[0-9]/) && password.match(/[!#$%.*&]/)){
40+
return true;
41+
} else {
42+
return false;
43+
}
44+
}
45+
);
46+
//console.log(checkPassword);
47+
return checkPassword;
48+
}
2849

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

week-3/3-extra/3-sorting-algorithm.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ You don't have to worry about making this algorithm work fast! The idea is to ge
1515
*/
1616

1717

18-
function sortAges(arr) {}
18+
function sortAges(arr) {
19+
let output=arr.filter(function(value){
20+
if(typeof value==="number"){
21+
//console.log(value);
22+
return value;
23+
}
24+
}).sort(function(a,b){
25+
return a-b;
26+
});
27+
//console.log(output);
28+
return output;
29+
}
1930

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

week-3/3-extra/card-validator.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function isValidateCard(cardNumber){
2+
3+
let numArray=cardNumber.split("");
4+
//return 16 if card contain same number
5+
let sameNumber=numArray.filter(function(num){
6+
return num===numArray[0];
7+
//&& num.match(/[0-9]/) &&
8+
//numArray.length===16;
9+
}).length;
10+
//retum the sum of all araay items
11+
let sumArray=numArray.reduce(function(total,num){
12+
return parseFloat(total)+parseFloat(num);
13+
});
14+
//check all number from 0-9
15+
let numberArray=numArray.every(function(num){
16+
return num.match(/[0-9]/)
17+
})
18+
//apply all rules to check valid card number
19+
if(numArray.length===16 && numberArray===true
20+
&&sameNumber<16 && numArray[numArray.length-1]%2===0
21+
&&sumArray>16)
22+
{
23+
return true;
24+
}
25+
else{
26+
isValid=false;
27+
}
28+
29+
30+
}
31+
//main program send card number to isValidateCard(cardNumber) function check the number
32+
let cardNumber="6666666666666661";
33+
var isValid=isValidateCard(cardNumber);
34+
if(Boolean(isValid)){
35+
console.log("credit card number is valid");
36+
}
37+
else{
38+
console.log("credit card number is not valid");
39+
}

0 commit comments

Comments
 (0)