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

Javascript/week3/hadiyah #87

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
19 changes: 16 additions & 3 deletions week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@

// write your code here

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

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

//var longNameThatStartsWithA = findLongNameThatStartsWithA(names);
var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA);
function findLongNameThatStartsWithA(name) {
return name.length > 7 && name[0] === "A";
}
console.log(longNameThatStartsWithA);

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

var pairs = pairsByIndex.map(function(indexes) {
let pairs = pairsByIndex.map((indexes) => {
if (indexes === null) {
process.exit(1);
}
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
Expand Down
2 changes: 1 addition & 1 deletion week-3/1-exercises/C-array-every/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To check that each name is longer than 3 characters, you'd have to run this func

## `.every()`

_Searches through an array and returns true if every item satisifies the predicate function you provided. Otherwise, it returns false_.
_Searches through an array and returns true if every item satisfies the predicate function you provided. Otherwise, it returns false_.

```js
var studentNameLength = students.every(isAboveThreshold);
Expand Down
5 changes: 4 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,8 +5,11 @@
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
var group = ["Austine", "Dany", "Swathi", "Daniel"];

var groupIsOnlyStudents; // complete this statement
var groupIsOnlyStudents = group.every(comparison); // complete this statement

function comparison(name) {
return students.includes(name);
}
if (groupIsOnlyStudents) {
console.log("The group contains only students");
} else {
Expand Down
6 changes: 4 additions & 2 deletions week-3/1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

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

var pairsByIndex; // Complete this statement
var pairsByIndex = pairsByIndexRaw.filter((item) => {
return Array.isArray(item) && item.length === 2;
}); // Complete this statement

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

var pairs = pairsByIndex.map(function(indexes) {
var pairs = pairsByIndex.map(function (indexes) {
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
Expand Down
15 changes: 15 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,18 @@

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

/*var multiplied = numbers.map(multiply);

function multiply(num) {
return num * 100;
}
//console.log(numbers.map(multiply));

console.log(numbers);
console.log(multiplied);*/

numbers.map(function (num) {
return num * 100;
});

console.log(numbers.map((num) => num * 100));
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 (num) {
if (num % 3 === 0 && num % 5 === 0) {
console.log("FizzBuzz");
} else if (num % 5 === 0) {
console.log("Buzz");
} else if (num % 3 === 0) {
console.log("Fizz");
} else {
console.log(num);
}
});

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

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

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

function capitalise(str) {}
function capitalise(str) {
let newWord = str.split("");
let firstLetter = newWord[0].toUpperCase();
newWord[0] = firstLetter;
return newWord.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
52 changes: 22 additions & 30 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
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.
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. It has produced an array of their Oxygen levels.

Expand All @@ -9,8 +8,11 @@ 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) {
let safePlanet = arr.find(
(item) => parseFloat(item) > 19.5 && parseFloat(item) < 23.5
);
return safePlanet;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -19,33 +21,23 @@ 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');
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}`);
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}`);
}

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

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

test(
"safeLevels function works - case 3",
safeLevels(oxygenLevels3),
"21.1%"
);
test("safeLevels function works - case 1", safeLevels(oxygenLevels1), "19.9%");

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

test("safeLevels function works - case 3", safeLevels(oxygenLevels3), "21.1%");
47 changes: 34 additions & 13 deletions week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,48 @@

Use the tests to confirm which message to return
*/
/*
function checkPink(fruit) {
return fruit === "pink";
}
function bushChecker(arr) {
let tester = arr.every(checkPink);
let txt = "";
if (tester == true) {
txt = "Bush is safe to eat from";
} else {
txt = "Toxic! Leave bush alone!";
}
return txt;
} */

function bushChecker() {

function bushChecker(arr) {
let testFruit = arr.every((item) => item === "pink");
if (testFruit === 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"]
let bushBerryColours2 = ["pink", "pink", "pink", "pink"]
let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"];
let bushBerryColours2 = ["pink", "pink", "pink", "pink"];

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

test(
Expand Down
39 changes: 22 additions & 17 deletions week-3/2-mandatory/3-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
NOTE: don't include any element that is not a "family".
*/

function colonisers() {

function colonisers(arr) {
let settlers = arr.filter(
(item, index, arr) => item[0] === "A" && arr[index].includes("family")
);
return settlers;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -26,24 +29,26 @@ const voyagers = [
"Asimov",
"Oscar family",
"Avery family",
"Archer family"
"Archer family",
];

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)}`;
}

console.log(`${test_name}: ${status}`);
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}`);
}

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