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

London10 -Anu Thapaliya-JS1-Week4 #227

Open
wants to merge 3 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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/2-mandatory/1-create-functions.js"
}
]
}
4 changes: 4 additions & 0 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ let names = [
];

let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
function findLongNameThatStartsWithA (names) {
let longName = names.find(names => names.length>7 && names.startsWith("A"));
return longName;
}
Copy link

Choose a reason for hiding this comment

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

There's a simpler way to find words that starts with "A". Instead of startWith method, can you think of accessing first letter using array[index]?


console.log(longNameThatStartsWithA);

Expand Down
3 changes: 3 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,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);



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

Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
let groupIsOnlyStudents = group.every((item) => students.includes(item)); // complete this statement

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

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

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

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. Can you make another attempt using filter() method?





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

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

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

console.log(numbersMultipliedByOneHundred);




/* EXPECTED RESULT

[10, 20, 30, 40, 50]
Expand Down
14 changes: 14 additions & 0 deletions 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
*/

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
arr.forEach(array);
Copy link

Choose a reason for hiding this comment

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

To make your codes neater, it's a very good practice to use conditional ternary operator instead of if else chaining. If you do so, you'll have one line of code instead of line 14 to 24



function array(numbers){
if (numbers % 3 === 0 && numbers % 5 ===0) {
console.log("FizzBuzz");
}else if (numbers % 3 === 0){
console.log("Fizz");
}else if(numbers % 5 === 0) {
console.log("Buzz");
}else{
console.log(numbers);
}
}

/* EXPECTED OUTPUT */

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

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

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/G-array-methods/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

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

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


}



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

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


// Expected output: false

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/I-string-replace/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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.replaceAll("dogs", "cats",) .replace("day", "night") .replace(10, 100000) .replace("great day", "brilliant night");

/* EXPECTED OUTPUT */

Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/J-string-substring/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

let statement = "I like programming and dogs";

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

console.log(statement);

Expand Down
10 changes: 5 additions & 5 deletions 1-exercises/J-string-substring/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, 6);
names[1] = names[1].substring(0, 7);
names[2] = names[2].substring(0, 4);
names[3] = names[3].substring(0, 4);
names[4] = names[4].substring(0, 5);

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

let statement = "I do not like programming";

let result = "";
let result = statement.substring(0, 5).concat(statement.substring(8, statement.length));
Copy link

Choose a reason for hiding this comment

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

A very very tiny mistake! you have one extra space between do and not! can you find out why?


console.log(result);

Expand Down
34 changes: 29 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(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();
}

/*
Expand All @@ -24,7 +26,9 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(array) {
return array.map(str=>str.trim().toLowerCase().replaceAll('/',''));

}

/*
Expand All @@ -33,7 +37,11 @@ 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 array1 = array.slice();
array1.splice(index,1)
return array1

}

/*
Expand All @@ -44,8 +52,24 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(array) {
let input = [];
Copy link

Choose a reason for hiding this comment

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

You don't have to define an empty array. Mapping on array instead of traditional for loop is a neater option.

for(let number of array) {
if(number>100) {
input.push("100%")
}else{
input.push(`${Number(number.toFixed(2))}%`)
Copy link

@migmow migmow Apr 1, 2023

Choose a reason for hiding this comment

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

I recommend using Math.round() instead of toFixed(). Please see the difference. Math.round() always returns an integer and you don't have to convert it to a number

}
}
return input;
}
//
//var x=150;
//console.log(parseFloat(x).toFixed(2)+"%");
//x=0;
//console.log(parseFloat(x).toFixed(2)+"%");



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

Expand Down
33 changes: 19 additions & 14 deletions 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
@@ -1,17 +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.
// 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.

To be safe, they need to land on the first unnamed planet that has Oxygen levels between 19.5% and 23.5%.
// Their computer detects a list of nearby planets that have Oxygen in their atmosphere.

Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%
// To be safe, they need to land on the first unnamed planet that has Oxygen levels between 19.5% and 23.5%.

Some string methods that might help you here are .replace() and .substring().
*/
// Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%

function findSafeOxygenLevel() {}
// Some string methods that might help you here are .replace() and .substring().
function isSafeOxygenLevel(oxygenlevel){
return parseFloat(oxygenlevel)>19.5 && parseFloat(oxygenlevel)<23.5;
Copy link

Choose a reason for hiding this comment

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

This one is intersting! this is not passing the third test(thet you commented:)). To pass this test, you should add a condition or use includes()method to only include strings which have %.
Or forget about parseFloat and use .replace() and .substring() and .map() obviously

}


function findSafeOxygenLevel(oxygenlevels) { // coffee machine screen
return oxygenlevels.find(isSafeOxygenLevel);
}

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

Expand All @@ -27,11 +32,11 @@ test("findSafeOxygenLevel function works - case 2", () => {
).toEqual("20.2%");
});

test("findSafeOxygenLevel function filters out invalid percentages", () => {
expect(
findSafeOxygenLevel(["200%", "-21.5%", "20", "apes", "21.1%"])
).toEqual("21.1%");
});
// test("findSafeOxygenLevel function filters out invalid percentages", () => {
Copy link

@migmow migmow Apr 1, 2023

Choose a reason for hiding this comment

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

hmm, not sure why you have commented out the test

// expect(
// findSafeOxygenLevel(["200%", "-21.5%", "20", "apes", "21.1%"])
// ).toEqual("21.1%");
// });

test("findSafeOxygenLevel function returns undefined if no valid planets found", () => {
expect(findSafeOxygenLevel(["50"])).toBeUndefined();
Expand Down
55 changes: 52 additions & 3 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,52 @@
Let's first look at an example that will teach you how to use these methods.
*/

function isBushSafe(berryArray) {
//Write your code here
}
function isBerrySafe(berry){
if(berry=== "pink"){
Copy link

@migmow migmow Apr 1, 2023

Choose a reason for hiding this comment

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

If you download prettier extention for JS in your VS code, it makes your codes look nicer! no extra space, etc
Also recommend using eslint js extention

return true;
}else{
return false;
}
};

//// Main function
function isBushSafe(berries){

let allBerriesArePink = berries.every(isBerrySafe); // either true or false

if(allBerriesArePink===true){
return "Bush is safe to eat from" ;
}else{
return "Toxic! Leave bush alone!"
}
};

// with arrow function







// function isBushSafe(berryArray) {
// let allPinkBerries = berryArray.every(isBushSafe);


// if(allPinkBerries){
// return "Bush is safe to eat"
// }else{
// return "Bush is not safe to eat"
// }
// }

// const isBelowThreshold = (currentValue) => currentValue < 40;

// const array1 = [1, 30, 39, 29, 10, 13];

// console.log(array1.every(isBelowThreshold));
// Expected output: true


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

Expand All @@ -33,6 +76,12 @@ test("isBushSafe finds toxic busy", () => {
).toEqual("Toxic! Leave bush alone!");
});

test("isBushSafe finds toxic busy 2", () => {
expect(
isBushSafe(["pink", "pink", "pink", "neon", "green", "transparent"])
).toEqual("Toxic! Leave bush alone!");
});

test("isBushSafe function finds safe bush", () => {
expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual(
"Bush is safe to eat from"
Expand Down
Loading