Created
July 7, 2021 10:22
-
-
Save AbudiMutamba/ac3b9365c6c83bc0288e5994424e4a2e to your computer and use it in GitHub Desktop.
Ablestate Corhort 1 javascript session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Array methods | |
*/ | |
/** | |
* array.concat(value1, value2,...valueN) | |
* Concatinates two or more arrays and returns a new array | |
* It also accepts values of different data types e.g strings, numbers etc | |
*/ | |
console.clear(); | |
let districts1 = ["kampala", "Jinja", "Mukono"]; | |
let districts2 = ["Mbarara", "Masaka"]; | |
//concate two arrays | |
let districts3 = districts1.concat(districts2); | |
console.log(districts3); | |
//concate other valaues to the array | |
let mixed = districts3.concat("Lwengo", 100, { name: "David"}); | |
console.log(mixed); | |
/** | |
* array.every(callbacks[, thisObject]) | |
* It returns true id every elements in the array satisifies the testing function. The testing function is passed as a callback. | |
*/ | |
const TEST_IF_STRING = element => typeof element === 'string' ? true : false | |
/* const TEST_IF_STRING = element => { | |
if (typeof elemenent === 'string') return true | |
return false; | |
} | |
*/ | |
console.clear(); | |
// const TEST_IF_STRING = (element) => { | |
// if (typeof element === 'string') return true; | |
// return false; | |
// } | |
let valueOfevery = districts1.every(TEST_IF_STRING) | |
console.log(valueOfevery); | |
valueOfevery = mixed.every(TEST_IF_STRING) | |
console.log(valueOfevery); | |
valueOfevery = mixed.every(function (elements){ | |
if (typeof element === "string") return true; | |
return false; | |
}); | |
console.log(""); | |
console.log(valueOfevery); | |
valueOfevery = mixed.every((element) => typeof element === "string" ? true : false); | |
console.log(); | |
console.log(valueOfevery); | |
valueOfevery = districts2.every((element) => typeof element === "string" ? true : false); | |
console.log(); | |
console.log(valueOfevery); | |
/** | |
* array.filter(callbacks[, thisobject]) | |
* Returns a new array containing the elements that satisify the conditions of the callbacks method | |
*/ | |
const RETURN_NUMBERS = element => typeof element === 'number' ? true : false | |
let getNumbers = mixed.filter(RETURN_NUMBERS) | |
console.log(getNumbers); | |
const RETURN_STRING = element => typeof element === `string` ? true : false | |
let getString = mixed.filter(RETURN_STRING) | |
console.log(getString); | |
// const RETURN_OBJECT = (element) => type element === "object" ? true : false; | |
// let getObject = mixed.filter(RETURN_OBJECT) | |
// console.log(getObject) | |
let getObject = mixed.filter((element) => typeof element === `object` ? true : false); | |
console.log(getObject) | |
/** | |
* array.forEach(callback[, thisObject]) | |
* It calls a callback function for each element in the array and returns undefined | |
*/ | |
console.clear(); | |
const DESCRIBE_ELEMENT = (element) => | |
console.log(`${typeof element} : ${element}`); | |
//console.log(mixed.forEach(DESCRIBE_ELEMENT)); | |
let holdObject = []; | |
mixed.forEach((element) => { | |
if ( typeof element === "object") holdObject.push(element); | |
}); | |
console.log(holdObject); | |
mixed.forEach((element) => { | |
if (typeof element === "object") mixed.pop(); | |
}); | |
console.log(mixed) | |
/** | |
* array.join(separator) | |
* Its joins array elements and returns a string | |
* Default the separator is a comma. | |
*/ | |
let districtString = districts1.join(", "); | |
console.log(districtString) | |
// console.log(districts1) | |
// for (let num = 0; num < districts1.length; num++){ | |
// console.log(`${num} : ${districts1[0],districts1[1]}`) | |
// } | |
districtString = districts1.join(" | "); | |
console.log(districtString) | |
/** | |
* array.map(callback[, thisObject]) | |
* It invokes a callback for each element in the array and returns an array | |
*/ | |
const DECORATE_ELEMENT = (element) => { | |
if ( typeof element !== "object") return `- ${element} -`; | |
}; | |
let decoratedElements = mixed.map(DECORATE_ELEMENT); | |
console.log(mixed); //Test if it mutates the orginal array | |
console.log(decoratedElements); | |
const generatedHTMLOptions = (element) => { | |
if ( typeof element !== "object") | |
return `<options value="${element}"> ${element} </options>`; | |
}; | |
let htmlOptions = mixed.map(generatedHTMLOptions) | |
console.log(htmlOptions); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment