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

LONDON_10 || SAIM KORKMAZ || JS1-4 #230

Open
wants to merge 11 commits 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((name) => {
return name.startsWith("A") && name.length > 7;
});
}

let names = [
"Rakesh",
Expand Down
7 changes: 7 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +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){
exit(1);
}
}
}

let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand Down
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
22 changes: 20 additions & 2 deletions 1-exercises/E-array-map/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@

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

[10, 20, 30, 40, 50]
*/
*/
22 changes: 17 additions & 5 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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good approach, filtering is an expensive operation that iterates over each element in the array. If array has n elements, it will result in n operations.

You can get the first 5 elements with 1 operation - arr.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(arr) {
return arr.sort();
}
Comment on lines +15 to 17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests for this function are failing because it modifies the passed in array. I'd suggest making a copy of the array and sorting that, e.g [...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);
Copy link

@berkeli berkeli Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as before, we should not use filter if we know which element we don't need:

const removedElement = arr.splice(index,1)
return arr // this will not contain removedElement

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

This modifies the source array, so if you want to avoid that - you can create a copy via const arr1 = [...arr];

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) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const formattedPercentages = numbers.map((number) => {
const formattedPercentages = arr.map((number) => {

numbers is not defined here and also your tests are not passing for this function.

I'd suggest using Math.round(num * 100) / 100 instead of toFixed()

if (number > 100) {
number = 100;
}
return (Math.round(number * 100) / 100).toFixed(2) + "%";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (Math.round(number * 100) / 100).toFixed(2) + "%";
return (Math.round(number * 100) / 100)+ "%";

toFixed is not needed here, it will already round to 2 decimal places.

});
return formattedPercentages;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
17 changes: 14 additions & 3 deletions 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(arr) {
//result = "";
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];
}
}
}

// console.log(
// findSafeOxygenLevel(["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"])
// );

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

test("findSafeOxygenLevel function works - case 1", () => {
test("findSafeOxygenLevel function works - case 1", () => {
expect(
findSafeOxygenLevel(["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"])
).toEqual("19.9%");
Expand All @@ -35,4 +46,4 @@ test("findSafeOxygenLevel function filters out invalid percentages", () => {

test("findSafeOxygenLevel function returns undefined if no valid planets found", () => {
expect(findSafeOxygenLevel(["50"])).toBeUndefined();
});
});
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!";
}
Comment on lines +26 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (berryArray.every(v => v == "pink")) {
return "Bush is safe to eat from";
} else {
return "Toxic! Leave bush alone!";
}
if (berryArray.every(v => v == "pink")) {
return "Bush is safe to eat from";
}
return "Toxic! Leave bush alone!";

We can skip the else statement for a cleaner code


}

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"
);
});
});
6 changes: 5 additions & 1 deletion 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")
));
Comment on lines +19 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why you need result here?

}

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

Expand Down
12 changes: 11 additions & 1 deletion 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
}
Comment on lines +10 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works fine, but since this week was focused on array methods - it would be nice if you can rewrite with .filter() and .map()


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

Expand Down
23 changes: 18 additions & 5 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
Comment on lines +132 to +138
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea here was to use the functions you wrote earlier (getLocationName and isAccessibleByTransportMode)

also, you can use.filter()and .map()

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
4 changes: 3 additions & 1 deletion 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 Down
50 changes: 47 additions & 3 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 Down