-
-
Notifications
You must be signed in to change notification settings - Fork 261
London10 -Anu Thapaliya-JS1-Week4 #227
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
} | ||
] | ||
} |
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 | ||
let pairsByIndex = pairsByIndexRaw.filter((pairs) => Array.isArray(pairs) && pairs.length ===2); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,20 @@ | |
*/ | ||
|
||
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; | ||
arr.forEach(array); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
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,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('/','')); | ||
|
||
} | ||
|
||
/* | ||
|
@@ -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 | ||
|
||
} | ||
|
||
/* | ||
|
@@ -44,8 +52,24 @@ Write a function that: | |
- Numbers greater 100 must be replaced with 100. | ||
*/ | ||
|
||
function formatPercentage() { | ||
function formatPercentage(array) { | ||
let input = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))}%`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ===== */ | ||
|
||
|
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 %. |
||
} | ||
|
||
|
||
function findSafeOxygenLevel(oxygenlevels) { // coffee machine screen | ||
return oxygenlevels.find(isSafeOxygenLevel); | ||
} | ||
|
||
/* ======= TESTS - DO NOT MODIFY ===== */ | ||
|
||
|
@@ -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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 ===== */ | ||
|
||
|
@@ -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" | ||
|
There was a problem hiding this comment.
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]?