DEV Community

Isabella
Isabella

Posted on

.reverse() | .split('') | .join('') and palindrome in JS Explained

1.Create a function that takes string.
Print the reverse of that string to the console.

let unoReverse = str => console.log(str.split('').reverse().join(''))

// .split(') takes the string and it returns it into an array
//once we have the array, reverse() **can reverse each
// in this case we
split('')** in individual letters ; .reverse() can take those individual letters and it can reverse them
//Once we reversed our array, we joined back together join('') to get back a string, otherwise we will still be dealing with an array and we want our string back.
// reverse() doesnt need quotes "" because when theyve made the reverse method they didn`t needed to take in an argument to do the reversing.

2.Create a function that takes in a string.
Alert if the string is a palindrome or not.
const palindromeCheck = str => alert(str === str.split('').reverse().join(''))
palindromeCheck('racecar')

//We are going to take in a string and see if it equals the same as that reversed. So if the str(string) equals the str reversed, we know that we have a palindrome. In this case will alert 'true'.

In simple terms, when the numbers, strings, or characters are reversed that return the same result as the original numbers or characters, it is called a Palindrome.

Top comments (0)