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 1 commit
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
Prev Previous commit
Next Next commit
completed all the exercises (non mandatory)
  • Loading branch information
sadatakhtar committed Jul 1, 2020
commit a26f8e8baf0f6b6f16ea04cb18458973ee9e7d5d
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,24 +5,18 @@
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);

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */

console.log(firstFive);
console.log(everyone);
console.log(lastFive);

/*
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
2 changes: 1 addition & 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,7 @@ To be safe, they need to land on the first unamed planet that has Oxygen levels
Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%
*/

function safeLevels() {
function safeLevels(arr) {

}

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
77 changes: 44 additions & 33 deletions week-3/2-mandatory/4-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,58 @@
(see tests to confirm how this data will be structured)
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents() {

}

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

const attendances = [
["Ahmed", 8],
["Clement", 10],
["Elamin", 6],
["Adam", 7],
["Tayoa", 11],
["Nina", 10]
]
];

function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;

for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}

return true;
}

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}

console.log(`${test_name}: ${status}`);
function eligibleStudents(arr) {
let examStudents = arr.filter(item => item.filter(item = item >= 8) );
return examStudents;

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

test("eligibleStudents function works",
arraysEqual(
eligibleStudents(attendances), ["Ahmed", "Clement", "Tayoa", "Nina"]
)
)
// const attendances = [
// ["Ahmed", 8],
// ["Clement", 10],
// ["Elamin", 6],
// ["Adam", 7],
// ["Tayoa", 11],
// ["Nina", 10]
// ]

// function arraysEqual(a, b) {
// if (a === b) return true;
// if (a == null || b == null) return false;
// if (a.length != b.length) return false;

// for (let i = 0; i < a.length; ++i) {
// if (a[i] !== b[i]) return false;
// }

// return true;
// }

// function test(test_name, expr) {
// let status;
// if (expr) {
// status = "PASSED";
// } else {
// status = "FAILED";
// }

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

// test("eligibleStudents function works",
// arraysEqual(
// eligibleStudents(attendances), ["Ahmed", "Clement", "Tayoa", "Nina"]
// )
// )