Given a string of characters as input, write a function that returns it with the characters reversed. (E.g "hello world" => "dlrow olleh").
SOLUTION 1:
#1. Let's create a function "reversedWord" with a parameter "w" which represents the argument (word to be reversed). Let's test it by returning the value of the argument and log it out to the console.
function reversedWord(w) {
return w;
}
console.log(reversedWord("Hello World"));
#2. Next, let's use the split method to make the string to become an array of the string characters.
function reversedWord(w) {
let arrOfChars = w.split("")
return arrOfChars;
}
console.log(reversedWord("Hello World"));
#3. Next, let's use the reverse method to reverse the order of the characters in the array.
function reversedWord(w) {
let arrOfChars = w.split("");
arrOfChars.reverse();
return arrOfChars;
}
console.log(reversedWord("Hello World"));
#4. Finally, let's convert the array back to a string by using the join method to return the reversed string.
function reversedWord(w) {
let arrOfChars = w.split("");
arrOfChars.reverse();
let reversedString = arrOfChars.join("");
return reversedString;
}
console.log(reversedWord("Hello World"));
SOLUTION 2:
#1. Let's create a function "reversedWord" with a parameter "w" which represents the argument (word to be reversed). Let's test it by returning the value of the argument and log it out to the console.
function reversedWord(w) {
return w;
}
console.log(reversedWord("Hello World"));
#2. Next, let's introduce a For of loop to iterate through the string and fetch each character in the string as "char". When we log out the value of "char", we will confirm that it fetches out each character in the string.
function reversedWord(w) {
for (let char of w) {
console.log(char);
}
}
console.log(reversedWord("Hello World"));
#3. Since we need the string in the reversed order, we will concatenate each of the characters to be arranged in the reversed order.
function reversedWord(w) {
let result = "";
for (let char of w) {
result = char + result;
}
return result;
}
console.log(reversedWord("Hello World"));
SOLUTION 3:
#1. First of all, let's make the string into an array of characters by using the split method.
function reversedWord(w) {
const wordSplit = w.split("");
return wordSplit;
}
console.log(reversedWord("Hello World"));
#2. Next, let's use a forEach method to loop through the array and make each character to be concatenated together in the reversed order. Then we will return the value of the 'reversedWords' as the result.
function reversedWord(w) {
const wordSplit = w.split("");
let reversedWords = "";
wordSplit.forEach((char) => {
reversedWords = char + reversedWords;
});
return reversedWords;
}
console.log(reversedWord("Hello World"));
SOLUTION 4:
#1. Let's create an array of the string characters with the split method and return the value.
function reversedWord(w) {
const wordSplit = w.split("");
return wordSplit;
}
console.log(reversedWord("Hello World"));
#2. Next, let's create a new variable 'wordSplitLength' which would store the value of the length of the array. Then, we would log the value out to confirm.
function reversedWord(w) {
const wordSplit = w.split("");
const wordSplitLength = wordSplit.length;
return wordSplitLength;
}
console.log(reversedWord("Hello World"));
#3. Next, we would use the array length to loop through the array and for each iteration, we would apply the pop method to remove the last element in the array and concatinate it to a variable 'reversedWords' to create a string of reversed words.
function reversedWord(w) {
const wordSplit = w.split("");
const wordSplitLength = wordSplit.length;
let reversedWords = "";
for(let i = 0; i < wordSplitLength; i++){
reversedWords += wordSplit.pop();
}
return reversedWords;
}
console.log(reversedWord("Hello World"));
SOLUTION 5:
#1. Let's find the string length of the passed argument and log out the result.
function reversedWord(w) {
const strlength = w.length;
return strlength;
}
console.log(reversedWord("Hello World"));
#2. Next, let's use the string length to loop through the string to get the string characters, starting from the end of the string (with index = [strLength - 1]) to the first character in the string. We would concatenate the result to a variable 'reversedString' and log out the result.
function reversedWord(w) {
const strlength = w.length;
let reversedString = "";
for (let i = strlength-1; i >= 0; i--) {
reversedString += w[i];
}
return reversedString;
}
console.log(reversedWord("Hello World"));
PLEASE NOTE: If you have another solution to the challenge and want to contribute, please comment below and I will add your code as one of the solutions, follow you, and accredit your contribution with a link to your page. Thank you! Let's learn together.
#End
Hope you enjoyed this! :) Follow me for more contents...
Get in Touch:
ifeanyiomeata.com
contact@ifeanyiomeata.com
Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2021