Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London_10-Anna_Hrychaniuk-JavaScript-Core-1-Coursework-Week4 #229

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
@@ -2,9 +2,6 @@
You are given an array of names.
Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters.
*/

// write your code here

let names = [
"Rakesh",
"Antonio",
@@ -17,6 +14,16 @@ let names = [
"Ahmed",
];

function ifLonger7(name) {
return name.length > 7 && name.startsWith('A');
}

function findLongNameThatStartsWithA(names) {
return names.find(ifLonger7);
}



let longNameThatStartsWithA = findLongNameThatStartsWithA(names);

console.log(longNameThatStartsWithA);
8 changes: 8 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,14 @@ let 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 isNegative(value) {
return value === null;
}

if (pairsByIndex.some(isNegative)) {
process.exit(1);
}


let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
6 changes: 5 additions & 1 deletion 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,11 @@
let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
function contain(name) {
return !students.includes(name);
}

let groupIsOnlyStudents = group.every(contain); // complete this statement

if (groupIsOnlyStudents) {
console.log("The group contains only students");
6 changes: 5 additions & 1 deletion 1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,11 @@

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

let pairsByIndex; // Complete this statement
function onlyNumbers (number) {
return Array.isArray(number) && number.length === 2;
}

let pairsByIndex = pairsByIndexRaw.filter(onlyNumbers); // Complete this statement

let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
8 changes: 7 additions & 1 deletion 1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,15 @@

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

let numbersMultipliedByOneHundred; // complete this statement
let numbersMultipliedByOneHundred = numbers.map(number => number*100);

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

let numbersMultipliedByHundred = numbers.map(number => {return number*100});// complete this statement

console.log(numbersMultipliedByOneHundred);
console.log(numbersMultiplied);
console.log(numbersMultipliedByHundred);

/* EXPECTED RESULT

15 changes: 15 additions & 0 deletions 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,21 @@

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

function fizzBuzz(item) {
if (item % 3 === 0 && item % 5 === 0){
item = "FizzBuzz";
}
else if (item % 3 === 0) {
item = "Fizz";
} else if (item % 5 === 0) {
item = "Buzz";
}
return item;
}

arr.map(fizzBuzz).forEach(number => console.log(number));


/* EXPECTED OUTPUT */

/*
2 changes: 1 addition & 1 deletion 1-exercises/G-array-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
*/

let numbers = [3, 2, 1];
let sortedNumbers; // complete this statement
let sortedNumbers = numbers.sort(); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
2 changes: 1 addition & 1 deletion 1-exercises/G-array-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
let mentors = ["Daniel", "Irina", "Rares"];
let students = ["Rukmini", "Abdul", "Austine", "Swathi"];

let everyone; // complete this statement
let everyone = mentors.concat(students); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
4 changes: 2 additions & 2 deletions 1-exercises/H-array-methods-2/exercise.js
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ let everyone = [
"Swathi",
];

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

/*
DO NOT EDIT BELOW THIS LINE
6 changes: 5 additions & 1 deletion 1-exercises/H-array-methods-2/exercise2.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,11 @@
Tip: use the string method .split() and the array method .join()
*/

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

/*
DO NOT EDIT BELOW THIS LINE
2 changes: 1 addition & 1 deletion 1-exercises/H-array-methods-2/exercise3.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"];

function isInUK(country) {
return; // complete this statement
return ukNations.includes(country); // complete this statement
}

/*
58 changes: 30 additions & 28 deletions 1-exercises/I-string-replace/exercise.js
Original file line number Diff line number Diff line change
@@ -13,34 +13,36 @@
let story =
"I like dogs. One day I went to the park and I saw 10 dogs. It was a great day.";

let result = story.replace("", "");
let result = story.replace("dogs", "cats").replace("10 dogs", "100000 cats");

/* EXPECTED OUTPUT */

const util = require("util");

function test(test_name, actual, expected) {
console.log("");
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: \nexpected: ${util.inspect(
expected
)} \nbut your function returned: ${util.inspect(actual)}`;
}

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

test(
"1. Original story has not been changed",
story,
"I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."
);

test(
"2. The result of the replace is correct",
result,
"I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night."
);
console.log(result);

// const util = require("util");

// function test(test_name, actual, expected) {
// console.log("");
// let status;
// if (actual === expected) {
// status = "PASSED";
// } else {
// status = `FAILED: \nexpected: ${util.inspect(
// expected
// )} \nbut your function returned: ${util.inspect(actual)}`;
// }

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

// test(
// "1. Original story has not been changed",
// story,
// "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."
// );

// test(
// "2. The result of the replace is correct",
// result,
// "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night."
// );
2 changes: 1 addition & 1 deletion 1-exercises/J-string-substring/exercise.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

let statement = "I like programming and dogs";

statement = statement.substring();
statement = statement.substring(0, 19);

console.log(statement);

10 changes: 5 additions & 5 deletions 1-exercises/J-string-substring/exercise2.js
Original file line number Diff line number Diff line change
@@ -14,11 +14,11 @@ let names = [
"Arron Graham",
];

names[0] = names[0].substring();
names[1] = names[1].substring();
names[2] = names[2].substring();
names[3] = names[3].substring();
names[4] = names[4].substring();
names[0] = names[0].substring(0, 7);
names[1] = names[1].substring(0, 7);
names[2] = names[2].substring(0, 5);
names[3] = names[3].substring(0, 5);
names[4] = names[4].substring(0, 6);

names.forEach((name) => {
console.log(name);
3 changes: 2 additions & 1 deletion 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@

let statement = "I do not like programming";

let result = "";
let result = `${statement.substring(0, 4)}${statement.substring(8, statement.length)} `;


console.log(result);

25 changes: 20 additions & 5 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
@@ -3,15 +3,17 @@ Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the first five elements of the passed array.
*/
function first5() {
function first5(array) {
return array.slice(0, 5);
}

/*
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the same elements, except sorted.
*/
function sortArray() {
function sortArray(array) {
return array.slice().sort();
}

/*
@@ -24,7 +26,8 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(array) {
return array.map(string => string.trim().replace("/", "").toLowerCase());
}

/*
@@ -33,9 +36,16 @@ Write a function that:
- Returns a new array containing the same elements, but without the element at the passed index.
*/

function remove() {
function remove(array, index) {
let newArray = array.slice();
let firstPart = array.slice(0,index);
let lastPart = array.slice(index+1, newArray.length)
return firstPart.concat(lastPart);
// return array.filter((_, i) => i !== index);
}



/*
Write a function that:
- Takes an array of numbers as input.
@@ -44,7 +54,12 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(array) {
return array.map(value => {
let notOver100 = Math.min(value, 100);
let roundedValue = Math.round(100*notOver100) / 100;
return `${roundedValue}%`
})
}

/* ======= TESTS - DO NOT MODIFY ===== */
14 changes: 13 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,19 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(planet) {
let checkPlanet = planet.filter(item => item.includes("%"));
let checkPlanet2 = checkPlanet.map(item => item.replace(/%/g, ""));
// checkPlanet2.map(item => parseFloat(item));
let checkPlanet3 = checkPlanet2.map(Number);
let checkPLanet4 = checkPlanet3.filter(item => item >19.5 && item < 23.5);
for (let num of checkPLanet4) {
if (19.5 < num && num < 23.5) {
return `${num}%`;
}
}
return checkPLanet4[0];
}

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

6 changes: 5 additions & 1 deletion 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
@@ -22,7 +22,11 @@
*/

function isBushSafe(berryArray) {
//Write your code here
if (berryArray.every(berry => berry === "pink")) {
return "Bush is safe to eat from"
} else {
return "Toxic! Leave bush alone!"
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
5 changes: 4 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
@@ -15,7 +15,10 @@

*/

function getSettlers() {}
function getSettlers(voyagers) {
let families = voyagers.filter(family => family.endsWith("family"));
return families.filter(family => family.startsWith("A"));
}

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

10 changes: 9 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,15 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}
function getEligibleStudents(students) {
let eligible = [];
for (let student of students) {
if (student[1] >= 8){
eligible.push(student[0]);
}
}
return eligible;
}

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

24 changes: 19 additions & 5 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
if (stringText.includes("code")) {
return stringText.indexOf("code");
} else {
return "Not found";
}
@@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) {
Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}
function getTransportModes(locations) {
return locations.slice(1);
}

/*
Implement the function isAccessibleByTransportMode that
@@ -81,7 +83,9 @@ function getTransportModes() {}
Hint: Use the corresponding array method to decide if an element is included in an array.
*/
function isAccessibleByTransportMode() {}
function isAccessibleByTransportMode(transportList, transport) {
return transportList.includes(transport);
}

/*
Implement the function getLocationName that
@@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {}
- Returns the name of the location
e.g: "Tower Bridge"
*/
function getLocationName() {}
function getLocationName(destination) {
return destination[0];
}

/*
We arrived at the final method. it won't take long if you use the previously implemented functions wisely.
@@ -122,6 +128,14 @@ function getLocationName() {}
Advanced challange: try to use arrow function when invoking an array method.
*/
function journeyPlanner(locations, transportMode) {
let places = [];
locations.forEach(element => {if(isAccessibleByTransportMode(element, transportMode)) {
places.push(getLocationName(element));
}

});

return places
// Implement the function body
}

5 changes: 4 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,10 @@
HINT: string and array methods that could be helpful (indexOf, filter)
*/

function getLanes() {}
function getLanes(streetList) {
return streetList.filter((name) => name.includes("Lane"));

}

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

5 changes: 4 additions & 1 deletion 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
@@ -23,7 +23,10 @@ PasswordValidationResult= [false, false, false, false, true]
*/

function validatePasswords(passwords) {}
function validatePasswords(passwords) {
return ifValid = passwords.map((password, index) => (index === passwords.indexOf(password)) && containsUppercaseLetter(password) && containsLowercaseLetter(password)
&& containsNumber(password) && containsSymbol(password) && password.length > 4);
}

// Returns true if string contains at least one uppercase letter.
function containsUppercaseLetter(string) {
19 changes: 18 additions & 1 deletion 3-extra/2-sorting-algorithm.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,24 @@ You don't have to worry about making this algorithm work fast! The idea is to ge
"think" like a computer and practice your knowledge of basic JavaScript.
*/

function sortAges(arr) {}
function sortAges(arr) {
let newArray = [];
for (let item of arr) {
if (typeof item == 'number') {
newArray.push(item);
}
}
for (let i=0; i<newArray.length-1; i++){
let num;
for (let j=0; j< newArray.length-i-1; j++)
if(newArray[j] > newArray[j+1]){
num = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = num;
}
}
return newArray;
}

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