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

Js/week3/denis p #80

Open
wants to merge 2 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/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

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

var longNameThatStartsWithA = findLongNameThatStartsWithA(names);
function findLongNameThatStartsWithA(names){
return names[0] === 'A' && names.length > 7;
}

var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA);

console.log(longNameThatStartsWithA);

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

function isNull(num){
return num === null;
}

var pairs = pairsByIndex.map(function(indexes) {
if(isNull(indexes)){
process.exit(1);
}
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
Expand Down
6 changes: 5 additions & 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,11 @@
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
var group = ["Austine", "Dany", "Swathi", "Daniel"];

var groupIsOnlyStudents; // complete this statement
function isInGroup(name){
return students.includes(name);
}

var groupIsOnlyStudents = group.every(isInGroup); // 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(item => Array.isArray(item) && item.length === 2) ; // Complete this statement

var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand Down
22 changes: 22 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,25 @@

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

let multiplyBy100 = numbers.map(item => item *100);
console.log(multiplyBy100);

/////////////////////////////////////////////
/////////////////////////////////////////////

function multiplyBy100_1(number){
return number * 100;
}

let numbersMultipliedBy100 = numbers.map(multiplyBy100_1);

console.log(numbersMultipliedBy100);

/////////////////////////////////////////////
/////////////////////////////////////////////

let numbersMultipliedBy100_1 = numbers.map(function(number){
return number * 100;
})

console.log(numbersMultipliedBy100_1);
17 changes: 17 additions & 0 deletions week-3/1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

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

arr.forEach(number => {
if(number % 3 === 0 && number % 5 === 0){
console.log('FizzBuzz') ;
}
else if(number % 3 === 0){
console.log('Fizz') ;
}
else if(number % 5 === 0){
console.log('Buzz') ;
}
else{
console.log(number);
}
})



/* 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(-5); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
9 changes: 7 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,13 @@
Tip: use the string method .split() and the array method .join()
*/

function capitalise(str) {}

function capitalise(str) {
let newStrArr = str.split('');
newStrArr[0] = newStrArr[0].toUpperCase();
return newStrArr.join('');

}

/*
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
49 changes: 33 additions & 16 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@
Many years into the future, a team of Space Voyagers find their ship is low on Oxygen and need to dock
somewhere safe while they call home for help.

Their computer detects a list of nearby planets that have Oxygen in their atmosphere.
Their computer detects a list of nearby planets that have Oxygen in their atmosphere. It has produced an array of their Oxygen levels.

To be safe, they need to land on the first unamed planet that has Oxygen levels between 19.5% and 23.5%.
To be safe to land on, a planet needs to have an Oxygen level between 19.5% and 23.5%.

Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%
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 firstSafeLevel = [];
arr.map(oxygen =>{
if(parseFloat(oxygen) > 19.5 && parseFloat(oxygen) < 23.5) {
firstSafeLevel.push(oxygen);
}
});
return firstSafeLevel[0];
}

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

const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]
const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"]
const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"];
const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"];
const oxygenLevels3 = ["200%", "21.1%"];

const util = require('util');

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

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

test(
"safeLevels function works - case 2",
safeLevels(oxygenLevels1) === "19.9%"
"safeLevels function works - case 1",
safeLevels(oxygenLevels1),
"19.9%"
);

test(
"safeLevels function works - case 2",
safeLevels(oxygenLevels2) === "20.2%"
);
safeLevels(oxygenLevels2),
"20.2%"
);

test(
"safeLevels function works - case 3",
safeLevels(oxygenLevels3),
"21.1%"
);
41 changes: 29 additions & 12 deletions week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,42 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function isPink(str){
return str === 'pink';
}

function bushChecker(arr) {
if(arr.every(isPink)){
return "Bush is safe to eat from";
}
return "Toxic! Leave bush alone!";
}
/* ======= TESTS - DO NOT MODIFY ===== */

let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"]
let bushBerryColours2 = ["pink", "pink", "pink", "pink"]

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

function test(test_name, actual, expected) {
let status;
if (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("bushChecker funtion works - case 1", bushChecker(bushBerryColours1) === "Toxic! Leave bush alone!")
test("bushChecker funtion works - case 1", bushChecker(bushBerryColours2) === "Bush is safe to eat from")
test(
"bushChecker funtion works - case 1",
bushChecker(bushBerryColours1),
"Toxic! Leave bush alone!"
);

test(
"bushChecker funtion works - case 1",
bushChecker(bushBerryColours2),
"Bush is safe to eat from"
);
52 changes: 29 additions & 23 deletions week-3/2-mandatory/3-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@
NOTE: don't include any element that is not a "family".
*/

function colonisers() {
function isFamily(arr){
let newArr = [];
arr.map(item => item.includes('family') ? newArr.push(item) : '');
return newArr;
}

function startsWithA(arr){
let newArr = [];
arr.map(item => item[0] === 'A' || item[0] === 'a' ? newArr.push(item) : '');
return newArr;
}

function colonisers(arr) {
let familyArray = isFamily(arr);
let colonisersFamilies = startsWithA(familyArray);
return colonisersFamilies;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -29,29 +43,21 @@ const voyagers = [
"Archer family"
];

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

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
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)}`;
}

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

test("colonisers function works",
arraysEqual(colonisers(voyagers), ["Adam family", "Avery family", "Archer family"])
)
test(
"colonisers function works",
colonisers(voyagers),
["Adam family", "Avery family", "Archer family"]
)
Loading