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

Glasgow Class 6 - Man Sang Sin - JavaScript Core 1 - Week 4 #226

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
52 changes: 47 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(inputArray) {
firstFiveNumbersArr = inputArray.slice(0, 5);
return firstFiveNumbersArr;
}

/*
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the same elements, except sorted.
*/
function sortArray() {
function sortArray(inputArray) {
let sortedArr = inputArray.sort();
return sortedArr;
}
Copy link

Choose a reason for hiding this comment

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

Well done !!
Please be aware that sort() method sorts the elements of an array and returns the reference to the same array.
Which means it will sort inputArray. To avoid this, you can try to copy the inputArray as value then sort the new array.
sortedArr = [...inputArray]; //it will copy the value of inputArray and not a reference to it.
sortedArr.sort( );

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Hope that helps. 😊
Keep up the good work 👌👌


/*
Expand All @@ -24,7 +28,21 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {

function removeSpaces(string) {
return string.trim();
}

function removeSlashes(string) {
return string.replace("/", "");
}

function makeLowercase(string) {
return string.toLowerCase();
}

function tidyUpString(inputArray) {
return inputArray.map(makeLowercase).map(removeSlashes).map(removeSpaces);
}

/*
Expand All @@ -33,7 +51,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(inputArray, index) {
let returnArr = [];
for (let i = 0; i < index; i++) {
returnArr.push(inputArray[i]);
}
for (let i = index + 1; i < inputArray.length; i++) {
returnArr.push(inputArray[i]);
}
return returnArr;
}

/*
Expand All @@ -43,8 +69,24 @@ Write a function that:
- The numbers must be rounded to 2 decimal places.
- Numbers greater 100 must be replaced with 100.
*/
function convertToPercentage(number) {
return `${number}%`;
}

function roundNumber(number) {
return Math.round(number * 100) / 100;
}

function belowhundred(number) {
if (number <= 100) {
return number;
} else {
return 100;
}
}

function formatPercentage() {
function formatPercentage(inputArray) {
return inputArray.map(belowhundred).map(roundNumber).map(convertToPercentage);
}

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

function findSafeOxygenLevel() {}
function removePercentage(percentString) {
percentString.replace("%", "");
}

function isAboveMinOxygenLevel(numberString) {
return 19.5 > Number(numberString);
}

function isBelowMaxOxygenLevel(numberString) {
return Number(numberString) < 23.5;
}

function findSafeOxygenLevel(input) {
validLevelsArr = input
.map(removePercentage)
.filter(isAboveMinOxygenLevel)
.filter(isBelowMaxOxygenLevel);
return validLevelsArr[0];
}

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

Expand Down
12 changes: 10 additions & 2 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
The .some() method tests to see if some of the values (at least 1) in an array
match what you're looking for and returns true or false.

The .every() method will only return true if all values match watch you're looking for.
The .every() method will only return true if all values match what you're looking for.

Let's first look at an example that will teach you how to use these methods.
*/

function isPink(colour) {
return colour === "pink";
}

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
13 changes: 12 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@

*/

function getSettlers() {}
function isFamily(element) {
return element.includes("family");
}

function isNameStartwithA(name) {
return name[0] === "A";
}

function getSettlers(listOfElements) {
let validFamilies = listOfElements.filter(isFamily).filter(isNameStartwithA);
return validFamilies;
}

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

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

function getEligibleStudents() {}
function isAttendanceOver8(studentRecord) {
return studentRecord[1] >= 8;
}

function returnName(studentRecord) {
return student[0];
}

function getEligibleStudents(studentList) {
return studentList.filter(isAttendanceOver8).map(returnName);
}

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

Expand Down
27 changes: 20 additions & 7 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Now let's do this small exercise

Using string methods update the checkCodeIsThere() function
- The function will have a string as a paramter
- The function will have a string as a parameter
- The function should check if the word "code" exists in the string
- If it does exist, return the index of it, if not return "Not found"

Expand All @@ -20,8 +20,8 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
if (stringText.includes(magicWord)) {
return stringText.IndexOf(magicWord);
} else {
return "Not found";
}
Expand Down Expand Up @@ -64,7 +64,10 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}
function getTransportModes(locationTransportArr) {
let lastIndex = locationTransportArr.length;
return locationTransportArr.splice(1, lastIndex);
}

/*
Implement the function isAccessibleByTransportMode that
Expand All @@ -81,7 +84,12 @@ function getTransportModes() {}

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

/*
Implement the function getLocationName that
Expand All @@ -92,7 +100,9 @@ function isAccessibleByTransportMode() {}
- Returns the name of the location
e.g: "Tower Bridge"
*/
function getLocationName() {}
function getLocationName(locationTransportArr) {
return locationTransportArr[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,7 +132,10 @@ function getLocationName() {}
Advanced challange: try to use arrow function when invoking an array method.
*/
function journeyPlanner(locations, transportMode) {
// Implement the function body
let accessibleLocations = locations
.filter((location) => location.includes(transportMode))
.map(getLocationName);
return accessibleLocations;
}

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