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

Week 3 #79

Open
wants to merge 4 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
7 changes: 5 additions & 2 deletions week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

// write your code here

var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"];
function isLongNameThatStartsWithA(name) {
return name[0] === "A" && name.length > 7;
}

var longNameThatStartsWithA = findLongNameThatStartsWithA(names);
var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"];
var longNameThatStartsWithA = names.find(isLongNameThatStartsWithA);

console.log(longNameThatStartsWithA);

Expand Down
8 changes: 8 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,15 @@ 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(x) {
return x === null;
}

var pairs = pairsByIndex.map(function(indexes) {
if(pairsByIndex.some(isNull)) {
console.log("Error: null value");
process.exit(1);
}
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
Expand Down
4 changes: 3 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,9 @@
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
var group = ["Austine", "Dany", "Swathi", "Daniel"];

var groupIsOnlyStudents; // complete this statement
var groupIsOnlyStudents = group.every(function isStudent(member) {
return students.includes(member);
});

if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand Down
9 changes: 8 additions & 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,14 @@

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

var pairsByIndex; // Complete this statement
var pairsByIndex = pairsByIndexRaw.filter(function (i) {
if (typeof(i) !== "object" || i === null) {
return false;}
else if (i.length !== 2) {
return false;}
else {
return true;}
});

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];

// method 1
var largerNumbers = numbers.map(function (x) {
return x * 100;
});
console.log(largerNumbers);


//method 2
function multiplyBy100(x) {
return x * 100;
}

var largerNumbers2 = numbers.map(multiplyBy100);
console.log(largerNumbers2);
12 changes: 12 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,18 @@

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

arr.forEach(function (n) {
if (n % 3 === 0 && n % 5 === 0) {
console.log("FizzBuzz");}
else if (n % 3 === 0) {
console.log("Fizz");}
else if (n % 5 === 0) {
console.log("Buzz");}
else {
console.log(n);}
});


/* 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(function(a, b) {return a - b}); // 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);

/*
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
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) {
str = str.split("");
str[0] = str[0].toUpperCase();
return str.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);
}

/*
Expand Down
51 changes: 36 additions & 15 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,60 @@
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(oxygenLevel) {
oxygenLevel = oxygenLevel.map(function (percentage) { //removes "%" from the string and converts it to a number
return Number(percentage.replace("%", ""));
});

for(let i = 0; i < oxygenLevel.length; ++i) {
if (oxygenLevel[i] > 19.5 && oxygenLevel[i] < 23.5) {
return String(oxygenLevel[i]).concat("%"); //converts the number back to string and appends "%"
}
}

return -1 // Error: no safe oxygen levels found
}

/* ======= 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%"
);
40 changes: 28 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,41 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function bushChecker(bush) {
for (let i = 0; i < bush.length; ++i) {
if (bush[i] !== "pink") {
return "Toxic! Leave bush alone!";
}
}
return "Bush is safe to eat from";
}

/* ======= 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"
);
46 changes: 22 additions & 24 deletions week-3/2-mandatory/3-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
NOTE: don't include any element that is not a "family".
*/

function colonisers() {

function colonisers(voyagers) {
let families = [];
for (let i = 0; i < voyagers.length; ++i) {
if (voyagers[i].includes("family") && voyagers[i][0] === "A") {
families.push(voyagers[i]);
}
}
return families;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -29,29 +35,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;
}
const util = require('util');

return true;
}

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"]
)
39 changes: 18 additions & 21 deletions week-3/2-mandatory/4-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents() {
function eligibleStudents(students) {
let eligibleStudents = [];

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

return eligibleStudents;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -22,31 +30,20 @@ const attendances = [
["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;
}
const util = require('util');

function test(test_name, expr) {
function test(test_name, actual, expected) {
let status;
if (expr) {
status = "PASSED";
if (util.isDeepStrictEqual(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("eligibleStudents function works",
arraysEqual(
eligibleStudents(attendances), ["Ahmed", "Clement", "Tayoa", "Nina"]
)
)
eligibleStudents(attendances),
["Ahmed", "Clement", "Tayoa", "Nina"]
);
Loading