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

London_Class_10 -Mehmet_Omer_Demir - JS-Core-1 - Week 4 #244

Open
wants to merge 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
*/

// write your code here
function findLongNameThatStartsWithA(names) {
return names.find(function(name) {
return name.startsWith("A") && name.length > 7;
});
}

let names = [
"Rakesh",
Expand Down
10 changes: 8 additions & 2 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ 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 checkNull(arr){
for (value of arr){
if (value == null){
process.exit(1);
}
}
}
let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

Expand All @@ -21,4 +27,4 @@ let pairs = pairsByIndex.map(function (indexes) {
return [student, mentor];
});

console.log(pairs);
console.log(pairs);
2 changes: 1 addition & 1 deletion 1-exercises/D-array-filter/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

let pairsByIndex; // Complete this statement
let pairsByIndex = pairsByIndexRaw.filter(pair => Array.isArray(pair) && pair.length == 2); // Complete this statement

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

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

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

// another solution
/* function hundredTimes(number){
return number * 100
}
let numbersMultipliedByOneHundred = numbers.map(hundredTimes); */

// another solution
//let numbersMultipliedByOneHundred = numbers.map(function hundredTimes(number){return number * 100});

// another solution
//let numbersMultipliedByOneHundred = numbers.map(function (number){return number * 100});

//another solution
let numbersMultipliedByOneHundred = numbers.map((number) => {
return number * 100;
});

// do not edit below
console.log(numbersMultipliedByOneHundred);

/* EXPECTED RESULT
Expand Down
24 changes: 18 additions & 6 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(arr) {
return arr.filter((element, index) => index < 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(arr) {
return arr.sort();
}

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

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

function remove() {
function remove(arr, index) {
const newArr = arr.filter((v, i) => index == i);
return newArr;
}

/*
Expand All @@ -44,7 +49,14 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(arr) {
const formattedPercentages = numbers.map((number) => {
if (number > 100) {
number = 100;
}
return (Math.round(number * 100) / 100).toFixed(2) + "%";
});
return formattedPercentages;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -142,4 +154,4 @@ test("formatPercentage function works", () => {
"100%",
"0.37%",
]);
});
});
9 changes: 8 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(arr) {

for (let i = 0; i < arr.length; i++) {
if ((arr[i].split("%"))[0] < 23.5 && (arr[i].split("%"))[0] > 19.5 && arr[i].includes("%")) {
return arr[i];
}
}
}

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

Expand Down
14 changes: 11 additions & 3 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@

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

}

console.log(isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"]));

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

test("isBushSafe finds toxic busy", () => {
test("isBushSafe finds toxic busy", () => {
expect(
isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"])
).toEqual("Toxic! Leave bush alone!");
Expand All @@ -37,4 +45,4 @@ test("isBushSafe function finds safe bush", () => {
expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual(
"Bush is safe to eat from"
);
});
});
8 changes: 6 additions & 2 deletions 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

*/

function getSettlers() {}
function getSettlers(arr) {
return (result = arr.filter(
(v) => v.startsWith("A") && v.includes("family")
));
}

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

Expand Down Expand Up @@ -43,4 +47,4 @@ test("getSettlers function works", () => {
"Archer family",
"A Great family",
]);
});
});
14 changes: 12 additions & 2 deletions 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}
function getEligibleStudents(arr) {
let result = [];

for (let i = 0; i < arr.length; i++) {
if (arr[i][1] > 7) {
result.push(arr[i][0])
}
}

return result
}

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

Expand All @@ -31,4 +41,4 @@ test("getEligibleStudents function works", () => {
test("getEligibleStudents function can return empty array", () => {
const attendance = [["Jacob", 7]];
expect(getEligibleStudents(attendance)).toEqual([]);
});
});
25 changes: 19 additions & 6 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
if (stringText.includes(magicWord)) {
return stringText.indexOf(magicWord);
} else {
return "Not found";
}
Expand Down Expand Up @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}
function getTransportModes(arr) {
return arr.splice(1, arr.length)
}

/*
Implement the function isAccessibleByTransportMode that
Expand All @@ -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(arr, str) {
return arr.includes(str)
}

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

/*
We arrived at the final method. it won't take long if you use the previously implemented functions wisely.
Expand Down Expand Up @@ -123,6 +129,13 @@ function getLocationName() {}
*/
function journeyPlanner(locations, transportMode) {
// Implement the function body
let result = [];
for (let i = 0; i < locations.length; i++) {
if (locations[i].includes(transportMode)){
result.push(locations[i][0])
}
}
return result
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -209,4 +222,4 @@ describe("journeyPlanner", () => {
"Tower Bridge",
]);
});
});
});
6 changes: 4 additions & 2 deletions 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
HINT: string and array methods that could be helpful (indexOf, filter)
*/

function getLanes() {}
function getLanes(arr) {
return arr.filter(v => v.includes("Lane"))
}

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

Expand All @@ -20,4 +22,4 @@ test("getLanes function works", () => {
];

expect(getLanes(streetNames)).toEqual(["Abchurch Lane", "Addle Lane"]);
});
});
52 changes: 48 additions & 4 deletions 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,57 @@ PasswordValidationResult= [false, false, false, false, true]

*/

function validatePasswords(passwords) {}
function validatePasswords(passwords) {
/* let result = [];
for (let i = 0; i < passwords.length; i++) {
if (
containsLowercaseLetter(passwords[i]) &&
containsUppercaseLetter(passwords[i]) &&
containsNumber(passwords[i]) &&
containsSymbol(passwords[i]) &&
passwords[i].length >= 5
) {
if (result.indexOf(passwords[i]) === -1) {
result.push(true);
} else {
result.push(false);
}
} else {
result.push(false);
}
}
return result; */
let result = [];
let seenPasswords = []; // create a new array to keep track of seen passwords
for (let i = 0; i < passwords.length; i++) {
if (
containsLowercaseLetter(passwords[i]) &&
containsUppercaseLetter(passwords[i]) &&
containsNumber(passwords[i]) &&
containsSymbol(passwords[i]) &&
passwords[i].length >= 5
) {
if (seenPasswords.indexOf(passwords[i]) === -1) {
result.push(true);
seenPasswords.push(passwords[i]); // add current password to seen passwords array
} else {
result.push(false);
}
} else {
result.push(false);
}
}
return result;
}
console.log(
validatePasswords(["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"])
);

// Returns true if string contains at least one uppercase letter.
function containsUppercaseLetter(string) {
return /[A-Z]/.test(string);
}

//console.log(containsUppercaseLetter("yUhbsdj"));
// Returns true if string contains at least one lowercase letter.
function containsLowercaseLetter(string) {
return /[a-z]/.test(string);
Expand All @@ -44,7 +88,7 @@ function containsNumber(string) {
function containsSymbol(string) {
return /[!#$%.*&]/.test(string);
}

console.log(containsSymbol);
/* ======= TESTS - DO NOT MODIFY ===== */

test("Example 1", () => {
Expand All @@ -69,4 +113,4 @@ test("Example 2", () => {
"Pl3nty!",
])
).toEqual([true, true, false, false, false]);
});
});