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

London-10-shadi-fakhri-JavaScript-Core-1-Coursework-Week4 #223

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
16 changes: 14 additions & 2 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
You are given an array of names.
Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters.
*/
function findLongNameThatStartsWithA(names){
let startA = names.charAt(0);
let longname = names.length;
if(startA === "A" && longname > 7 ){
return names;
}

}


// write your code here

let names = [
"Rakesh",
Expand All @@ -17,7 +25,11 @@ let names = [
"Ahmed",
];

let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
//let longNameThatStartsWithA =findLongNameThatStartsWithA(names);

let longNameThatStartsWithA = names.find(findLongNameThatStartsWithA);



console.log(longNameThatStartsWithA);

Expand Down
11 changes: 11 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ let pairs = pairsByIndex.map(function (indexes) {
});

console.log(pairs);

function checkArray(check){
if(check===null){
process.exit(1);
return "error"
}

}

let finalcheck = pairsByIndex.some(checkArray);
console.log(finalcheck);
6 changes: 5 additions & 1 deletion 1-exercises/C-array-every/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
This program should check if the array `group` contains only students
*/

function check(student){
return students.includes[student];
}

let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
let groupIsOnlyStudents = group.every(check);

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

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

let pairsByIndex; // Complete this statement


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

let pairsByIndex = pairsByIndexRaw.filter(item => Array.isArray(item) && item.length === 2);

let pairs = pairsByIndex.map(function (indexes) {
let student = students[indexes[0]];
let mentor = mentors[indexes[1]];
Expand All @@ -21,6 +23,10 @@ let pairs = pairsByIndex.map(function (indexes) {

console.log(pairs);



console.log(pairsByIndex);

/* EXPECTED RESULT

[ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ]
Expand Down
7 changes: 6 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,15 @@

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

let numbersMultipliedByOneHundred; // complete this statement
let numbersMultipliedByOneHundred = numbers.map(function multyple(number){
return number*100;
})

let numbersMultipliedByOneHundred = numbers.map(number => number * 100);

console.log(numbersMultipliedByOneHundred);


/* EXPECTED RESULT

[10, 20, 30, 40, 50]
Expand Down
19 changes: 19 additions & 0 deletions 1-exercises/F-array-forEach/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@
An array with numbers 1-15 has been provided.
*/


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


function test(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');
}
console.log(number);
}


arr.forEach(test);

/* 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)

/*
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 change = str.split("");
change[0] = change[0].toUpperCase();
let final = change.join("");
return final;
}


/*
DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 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,7 @@
let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"];

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

/*
Expand Down
5 changes: 4 additions & 1 deletion 1-exercises/I-string-replace/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
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.replace("dogs", "cats");
let result2 = result.replace("day", "night");
let result3 = result2.replace(10, 1000000);
let result4 = result3.replace("great day", "brilliant night");

/* EXPECTED OUTPUT */

Expand Down
16 changes: 11 additions & 5 deletions 1-exercises/J-string-substring/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
For example, if the name is "John Smith" you should return "John"
*/

function firstName(names){

}



let names = [
"Tamzin Lindsay",
"Jessica Tate",
Expand All @@ -14,11 +20,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
57 changes: 50 additions & 7 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,37 @@ 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) {
let newarr = arr.slice(0, 5);
return newarr;
}








/*
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.slice().sort();

}








/*
NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week.
Think about what is better about this solution than your one last week, and what is worse.
Expand All @@ -24,16 +44,30 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {

function tidyUpString(arr) {

Choose a reason for hiding this comment

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

Good job with this function. Just a minor comment, try to give more meaningful names to the variables and avoid using keywords like 'array' or 'string' as those can get confused with the type.

newarr = arr.map(function change(string) {
string = string.trim();
string = string.replace("/", "");
string = string.toLowerCase();
return string;
});
return newarr;
}

/*
Write a function that:
- Takes an array and an index as input.
- Returns a new array containing the same elements, but without the element at the passed index.
*/

function remove() {
function remove(arr, index) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (i !== index) {
result.push(arr[i]);
}
}
return result;
}

/*
Expand All @@ -44,9 +78,18 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
}
function formatPercentage(numbers) {
let newnumbers = numbers.map(function (number) {
if (number > 100) {
number = "100%";
} else {
number = `${Math.round(number * 100) / 100}%`;
}

return number;
});
return newnumbers;
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("first5 function works for more than five elements", () => {
Expand Down
17 changes: 16 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(arr) {
let newArr = arr.filter(function (oxygen) {
let newoxygen = oxygen.substring(0, oxygen.length - 1);
let oxygennumber = parseFloat(newoxygen);

if (oxygennumber > 0) {
if (!isNaN(oxygennumber) && oxygennumber > 19.5 && oxygennumber < 23.5) {
return oxygen;
}
}
});

let result = newArr[0];
return result;
}


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

Expand Down
8 changes: 7 additions & 1 deletion 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
*/

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

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
11 changes: 9 additions & 2 deletions 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@

*/

function getSettlers() {}

function getSettlers(arr) {
let newarr = arr.filter(function (voyager) {
let last6letter = voyager.substring(voyager.length - 6, voyager.length);
if (voyager[0] === "A" && last6letter === "family") {
return voyager;
}
});
return newarr;
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("getSettlers function works", () => {
Expand Down
11 changes: 10 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}
function getEligibleStudents(arr) {
const newarr = [];
for (let name of arr) {
if (name[1] >= 8) {
newarr.push(name[0]);
}

}
return newarr;
}

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

Expand Down
Loading