Created
July 2, 2021 10:30
-
-
Save AbudiMutamba/6f029209aef14172c4cd6fd17f3e44b4 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
/** | |
*String properties and methods continued | |
*/ | |
/* | |
*string.length: Returns the number of characters in the string. They include alphabets, spaces, punctuation and everything that is included in the string. | |
*/ | |
let exampleString = "Tugezesa strings mu JavaScript"; | |
const obuwanvuBwaString = exampleString.length; | |
console.log(obuwanvuBwaString ); | |
exampleString = `'{}{}/;'.=+_-!$`; | |
const obuwanvuBwaString2 = exampleString.length; | |
console.log(obuwanvuBwaString2); | |
console.log(exampleString); | |
/** | |
* string.replace(regexToFind, repkacementString/Function, flags ) | |
* string.replace(stringToFind, repkacementString/Function, flags ) | |
*/ | |
//Replace Tugea wwith Trying out | |
console.clear(); | |
exampleString = "Tugezesa strings mu JavaScript" | |
exampleString = exampleString.replace("Tugezesa", "Trying out") | |
console.log(exampleString); | |
/** | |
* string.split(separator, limit): Splits a string into chunks based on the separator and returns an array of characters | |
* If the separator is not definited the given string will be return as the only value of the new array. | |
*/ | |
const SPLIT_STRING = exampleString.split(); | |
console.log(SPLIT_STRING); | |
const SPLIT_STRING_SEPARATED_BY_SPACE = exampleString.split(" "); | |
console.log(SPLIT_STRING_SEPARATED_BY_SPACE); | |
const SPLIT_STRING_BY_INTO_LETTERS = exampleString.split(" "); | |
console.log(SPLIT_STRING_BY_INTO_LETTERS); | |
const SPLIT_STRING_SEPARATED_BY_SPACE_LIMIT= exampleString.split(" ", 4); | |
console.log(SPLIT_STRING_SEPARATED_BY_SPACE_LIMIT); | |
const SPLIT_STRING_SEPARATED_BY_A= exampleString.split("a"); | |
console.log(SPLIT_STRING_SEPARATED_BY_A); | |
//Challenge reverse the string | |
/** | |
* ------ | |
*1. Separate the string by letters to get an array | |
*2. Loop the elements of the Array while concatinating them to the reverse string | |
*3. Return the reversed string | |
*4. Display the string | |
*/ | |
let reversedString = ''; // 0(1) | |
let position = SPLIT_STRING_BY_INTO_LETTERS.length - 1; //0(1) | |
for (position; position >= 0; position--){ //0(n) 0(2n + 2) => 0(n) linear | |
reversedString += SPLIT_STRING_BY_INTO_LETTERS[position] // 0(1) => | |
} | |
console.log(reversedString); | |
//Using while | |
/** | |
* Quiz: Using comments describe what the functionbwlow does. Describe each line in the function | |
*Submit your answer before the end of the today | |
*@function REVERSE_STRING | |
*@param {string} string | |
*@loop - while | |
*@param {string} name | |
*@param {string} lastPosition | |
*@param {string} reversedString | |
*@param {string} SPLIT_STRING_BY_INTO_LETTERS | |
*@param {string} nameReversed | |
* | |
*/ | |
const REVERSE_STRING = (string) =>{ //Arrow function inittalization with a parameter called string | |
if (string == "" || string == null || string == 0) return string; //Returns the value or input passed to the string parameter through checking the | |
const SPLIT_STRING_BY_INTO_LETTERS = string.split(""); //Returns an array of character splitted into chunks | |
let lastPosition = SPLIT_STRING_BY_INTO_LETTERS.length - 1; // returns the number of characters in a string and substracts one assigning the result to lastPosition | |
let reversedString = ""; //inittalization of the string | |
while(lastPosition >= 0){ // condition executes if the value of lastPosition is greater or equal to zero; | |
reversedString += SPLIT_STRING_BY_INTO_LETTERS[lastPosition]; //concatinating of the | |
lastPosition--; //subtract one from the value passed to the lastPosition | |
} | |
return reversedString; // returns "" if the while loop is not exactude | |
} | |
let name = "Wampamba"; | |
let nameReversed = REVERSE_STRING(name); //assigning the name identifier to the function | |
console.log(nameReversed); | |
console.log(REVERSE_STRING("DA")); | |
console.log(REVERSE_STRING()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment