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

glasgow class 6-siver omar-javascript 1-week4 #234

Open
wants to merge 2 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
11 changes: 8 additions & 3 deletions 1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

// write your code here

let names = [
"Rakesh",
"Antonio",
Expand All @@ -16,10 +15,16 @@ let names = [
"Karim",
"Ahmed",
];
// === :
function findLongNameThatStartsWithA(array){
const findWords = array.find(word => word.length>7 && word[0]==='A')
return findWords;
}

let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
// let longNameThatStartsWithA = findLongNameThatStartsWithA(names);
// console.log(longNameThatStartsWithA);

console.log(longNameThatStartsWithA);
console.log(findLongNameThatStartsWithA(names));

/* EXPECTED OUTPUT */
// "Alexandra"
12 changes: 12 additions & 0 deletions 1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

function program(number){
if (number===null){
process.exit(1);
}
}
pairsByIndex.some(program);

let pairs = pairsByIndex.map(function (indexes) {
let student = students[indexes[0]];
let mentor = mentors[indexes[1]];
return [student, mentor];
});

console.log(pairs);





7 changes: 6 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,12 @@
let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"];
let group = ["Austine", "Dany", "Swathi", "Daniel"];

let groupIsOnlyStudents; // complete this statement
function checkGroup(groups){
return groups.includes(students);

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

if (groupIsOnlyStudents) {
console.log("The group contains only students");
Expand Down
3 changes: 2 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,8 @@

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

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

let students = ["Islam", "Lesley", "Harun", "Rukmini"];
let mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
Expand Down
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,8 +3,8 @@

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

let numbersMultipliedByOneHundred; // complete this statement

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

/* EXPECTED RESULT
Expand Down
10 changes: 10 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,16 @@

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

arr.forEach(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();// 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 =mentors.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);
let lastFive = everyone.slice(everyone.length - 5, everyone.length);

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

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

/*
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
6 changes: 5 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,11 @@
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;
result = result.replace(/dogs/gi, "cats");
result = result.replace(/day/gi, "night");
result = result.replace(/10/gi, "100000");
result = result.replace(/great/gi, "brilliant");

/* 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
4 changes: 2 additions & 2 deletions 1-exercises/J-string-substring/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

let statement = "I do not like programming";

let result = "";

let result =
statement.substring(0, 4) + statement.substring(8, statement.length);
console.log(result);

/* EXPECTED OUTPUT
Expand Down
34 changes: 28 additions & 6 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ 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();
}

/*
Expand All @@ -24,7 +27,14 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(arr) {
newArr = arr.map(function (string) {
string = string.trim();
string = string.replace("/", "");
string = string.toLowerCase();
return string;
});
return newArr;
}

/*
Expand All @@ -33,9 +43,15 @@ 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) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (i != index) {
newArr.push(arr[i]);
}
}
return newArr;
}

/*
Write a function that:
- Takes an array of numbers as input.
Expand All @@ -44,7 +60,13 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
18 changes: 15 additions & 3 deletions 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@

Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}

function findSafeOxygenLevel(oxygenLevels) {
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 ===== */

test("findSafeOxygenLevel function works - case 1", () => {
Expand Down
7 changes: 7 additions & 0 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

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

}
}

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

*/

function getSettlers() {}

function getSettlers(allPeople) {
return allPeople.filter(person => person.includes('family') && person.startsWith('A'));
}
/* ======= TESTS - DO NOT MODIFY ===== */

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

function getEligibleStudents() {}
function getEligibleStudents(students) {
const eligibleStudents= students.filter(attendance => attendance[1]>=8)
return eligibleStudents.map(names=> names[0]);
}

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

Expand Down
29 changes: 16 additions & 13 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
} else {
return "Not found";
}
}
if (stringText.includes(magicWord)) {
return stringText.indexOf(magicWord)
}else {
return "Not found";
}}

/*
I am new to London and would like to know what transport I can take to different famous locations.
Expand Down Expand Up @@ -64,8 +63,9 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}

function getTransportModes(londonLocations) {
return londonLocations.slice(1)
}
/*
Implement the function isAccessibleByTransportMode that

Expand All @@ -81,7 +81,9 @@ function getTransportModes() {}

Hint: Use the corresponding array method to decide if an element is included in an array.
*/
function isAccessibleByTransportMode() {}
function isAccessibleByTransportMode(transportModes, transport) {
return transportModes.includes(transport);
}

/*
Implement the function getLocationName that
Expand All @@ -92,8 +94,9 @@ function isAccessibleByTransportMode() {}
- Returns the name of the location
e.g: "Tower Bridge"
*/
function getLocationName() {}

function getLocationName(locations) {
return locations.at(0)
}
/*
We arrived at the final method. it won't take long if you use the previously implemented functions wisely.

Expand Down Expand Up @@ -122,8 +125,8 @@ function getLocationName() {}
Advanced challange: try to use arrow function when invoking an array method.
*/
function journeyPlanner(locations, transportMode) {
// Implement the function body
}
const accessibleLocations= locations.filter( el=> el.includes(transportMode))
return accessibleLocations.map(getLocationName)}

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

Expand Down
Loading