Created
July 2, 2021 10:26
-
-
Save AbudiMutamba/10c0d207eb29fb5c7e08f437d5d06ce8 to your computer and use it in GitHub Desktop.
Code Explanation
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
//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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment