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

Js core1 week3 jacques #88

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

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

var longNameThatStartsWithA = findLongNameThatStartsWithA(names);

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

console.log(nameFound);
/* EXPECTED OUTPUT */
// "Alexandra"
2 changes: 1 addition & 1 deletion week-3/1-exercises/B-array-some/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var numbers = [1, 3, -1, 5, 9];

You know that the array is supposed to contain positive numbers, but you want to check if it also contains any negative numbers.

We can write a function that checks this:
We can write a function that checks this:

```js
function isNegative(number) {
Expand Down
24 changes: 0 additions & 24 deletions week-3/1-exercises/B-array-some/exercise.js

This file was deleted.

3 changes: 2 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,8 @@
var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
var group = ["Austine", "Dany", "Swathi", "Daniel"];

var groupIsOnlyStudents; // complete this statement

var groupIsOnlyStudents = group.every(test => students.includes(test)); // complete this statement

if (groupIsOnlyStudents) {
console.log("The group contains only students");
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(function(value){
return ;
});// 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
17 changes: 17 additions & 0 deletions week-3/1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@
// Write multiple solutions using different syntax (as shown in the README)

var numbers = [0.1, 0.2, 0.3, 0.4, 0.5];
let numbersByHundred = function (number) {
return number * 100;
};
let result = numbers.map(numbersByHundred);
console.log(result);

let numbersHundred = numbers.map(function hundred(number) {
return number * 100;
});
console.log(numbersHundred);

let numHundred = numbers.map(function (number) {
return number * 100;
});
console.log(numHundred);

let byHundred = numbers.map(number => number * 100);
console.log(byHundred);
52 changes: 24 additions & 28 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/*
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.

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 first safe oxygen level in the array - Oxygen between 19.5% and 23.5%
*/

function safeLevels() {
function safeLevels(oxygenLevel) {
for (let i =0; i< oxygenLevel.length; i++) {
oxygenLevel[i] = parseFloat(oxygenLevel[i]);
if (oxygenLevel[i] > 19.5 && oxygenLevel[i] < 23.5) {
return `${oxygenLevel[i]}%`;

}
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -19,33 +25,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%");
33 changes: 20 additions & 13 deletions week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,33 @@
Use the tests to confirm which message to return
*/

function bushChecker() {

function bushChecker(items) {
for (let item of items) {
if (item !== '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"]
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
10 changes: 8 additions & 2 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(names) {
let newNames = [];
for (let i = 0; i < names.length; i++) {
if (names[i][0] === 'A' && names[i].includes('family')) {
newNames.push(names[i]);
}
}
return newNames;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
44 changes: 27 additions & 17 deletions week-3/2-mandatory/4-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents() {

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -19,23 +25,27 @@ const attendances = [
["Elamin", 6],
["Adam", 7],
["Tayoa", 11],
["Nina", 10]
]
["Nina", 10],
];

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("eligibleStudents function works",
eligibleStudents(attendances),
["Ahmed", "Clement", "Tayoa", "Nina"]
);
test("eligibleStudents function works", eligibleStudents(attendances), [
"Ahmed",
"Clement",
"Tayoa",
"Nina",
]);
9 changes: 8 additions & 1 deletion week-3/2-mandatory/5-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
NOTE: only the names should be returned, not the means of transport.
*/

function journeyPlanner() {
function journeyPlanner(locationsArr, transport) {
let newArr = [];
for (let i=0; i<locationsArr.length; i++){
if (locationsArr[i].includes(transport)) {
newArr.push(locationsArr[i][0]);
}
}
return newArr;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
51 changes: 29 additions & 22 deletions week-3/2-mandatory/6-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@
Write a function that will return all street names which contain 'Lane' in their name.
*/

function getLanes() {

function getLanes(streets) {
let streetWithLane = [];
for (let street of streets) {
if (street.includes("Lane")) {
streetWithLane.push(street);
}
}
return streetWithLane;
}

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

const streetNames = [
"Abchurch Lane",
"Adam's Court",
"Addle Hill",
"Addle Lane",
"Alban Highwalk"
]
"Abchurch Lane",
"Adam's Court",
"Addle Hill",
"Addle Lane",
"Alban Highwalk",
];

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(
"getLanes function works",
getLanes(streetNames),
["Abchurch Lane", "Addle Lane"]
);
test("getLanes function works", getLanes(streetNames), [
"Abchurch Lane",
"Addle Lane",
]);