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

London 10 | Shahid Amin | JavaScript-Core-1-Coursework-Week4 #224

Open
wants to merge 20 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
8 changes: 7 additions & 1 deletion 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ let names = [
"Karim",
"Ahmed",
];

/*
let longNameThatStartsWithA = findLongNameThatStartsWithA(names);

console.log(longNameThatStartsWithA);

const found = names.find(element => element[0] === "A" );*/



const found = names.find(element => element[0] === "A" && element.length > 7 );

/* EXPECTED OUTPUT */
// "Alexandra"
6 changes: 6 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
- Do not edit any of the existing code
*/

for (const pair of pairsByIndex) {
if (!pair) {
process.exit(1);
}
}

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
Expand Down
8 changes: 7 additions & 1 deletion 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
let groupIsOnlyStudents = function () {
for (let i = 0; i < students.length; i++) {
if (!group.includes(student[i])) {
return true;
}
}
}; // complete this statement

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

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

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

let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand All @@ -24,4 +26,4 @@ console.log(pairs);
/* EXPECTED RESULT

[ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ]
*/
*/
4 changes: 2 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,11 @@

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

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

console.log(numbersMultipliedByOneHundred);

/* EXPECTED RESULT

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

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

arr.forEach((number) => {
if (number % 3 === 0 && number % 5 === 0) {
console.log("FizzBuzz");
} else if (number % 3 === 0) {
console.log("Fizz");
} else if (number % 5 === 0) {
console.log("Buzz");
} else {
console.log(number);
}
});

console.log(filteredNumbers);
/* 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(); // complete this statement

/*
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 = mentor.concat(...students); // complete this statement

/*
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); // complete this statement
let lastFive = everyone.slice(-5); // complete this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
8 changes: 7 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,13 @@
Tip: use the string method .split() and the array method .join()
*/

function capitalise(str) {}
function capitalise(str) {
let strString = str.split("")
let strFirst = strString[0].toUpperCase()
let strCombined = strFirst.concat(str.slice(1))
return strCombined

}

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

function isInUK(country) {
return; // complete this statement
if (ukNations.includes(country)){
return true
}
else{
return "Not a UK country"
}
}

/*
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
22 changes: 22 additions & 0 deletions 1-exercises/J-string-substring/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ names.forEach((name) => {
console.log(name);
});

/*Other function j

function getFirstNames (){
let splitNames =[]
for(let i =0; i<names.length; i++){
splitNames.push(names[i].split(" "))
}

console.log(splitNames)

let firstNames = []

for(let i =0; i<splitNames.length; i++){
firstNames.push(splitNames[i][0])
}

console.log(firstNames)
//returns names as an array
}

*/

/* EXPECTED OUTPUT

"Tamzin"
Expand Down
15 changes: 13 additions & 2 deletions 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
*/

let statement = "I do not like programming";

let result = "";

console.log(result);
let wordToRemove = "not";

let index = statement.indexOf(wordToRemove);

if (index !== -1) {
let length = wordToRemove.length;

let firstPart = statement.substring(0, index);
let secondPart = statement.substring(index + length);

result = firstPart + secondPart;

console.log(result);
}
/* EXPECTED OUTPUT

"I do like programming"
Expand Down
55 changes: 50 additions & 5 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ 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) {
for (let i =0; i < 5 ; i++ ){
return arr[i]
}

/*
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) {
let arrSorted = arr.sort()
return arrSorted
}

/*
Expand All @@ -24,7 +28,11 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(str) {
trimStr = str.trim();
mystring = trimStr.replace('/','');
lowerStr = mystring.toLowerCase()

}

/*
Expand All @@ -33,7 +41,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) {
arr.splice(index, 1);
return arr;
}

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

function formatPercentage() {
/*function formatPercentage(arr) {
let arrD = []
let arrE = []
let arrB = arr.toString()
console.log(arrB)
let arrC = arrB.split(",")
console.log(arrC)
for (let i = 0; i < arrC.length; i++){
arrD.push(arrC[i] + "!")
}

console.log(arrD)

for (let num of arrD){
if (num.length > 3){
arrE.push('100%')
}
else{
arrE.push(num)
}


}

formatPercentage([1,2,1000,5,6,34])*/

function formatPercentage(arr) {
let result = [];
for (const number of arr) {
if (number > 100) {
result.push('100%')
} else {
result.push(`${Number(number.toFixed(2))}%`)
}
}
return result;
}

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

function findSafeOxygenLevel() {}
function findSafeOxygenLevel() {
let firstProperPlanet = "" ;
for (const oxygenlevel of oxygenLevels) {
if(oxygenlevel.includes('%')) {
const oxygenlevelNum = Number(oxygenlevel.replace('%', ''));
if (oxygenlevelNum > 19.5 && oxygenlevelNum < 23.5){
firstProperPlanet = oxygenlevel;
break
}} else {
continue
}
}
let output = firstProperPlanet === "" ? undefined : firstProperPlanet;
return output;
}

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

Expand Down
11 changes: 11 additions & 0 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@
*/

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

const bushIsSafe = berryArray.every((color) => color == "pink");
const output = bushIsSafe
? "Bush is safe to eat from"
: "Toxic! Leave bush alone!";
return output;

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

test("isBushSafe finds toxic busy", () => {
Expand Down
4 changes: 3 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

*/

function getSettlers() {}
function getSettlers() {
return allpeople.filter(person => person.includes('family') && person.startsWith('A'));
}

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

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

function getEligibleStudents() {}
function getEligibleStudents() {
let examClass = [];
for (student in attendance){
if student[1]
}
}

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

Expand Down
Loading