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
/* | |
Reverse a number. | |
*/ | |
function reverseInt(num) { | |
//Convert the num to a string, reverse the chars. | |
const reversedStr = num.toString().split('').reverse().join(''); | |
//Convert back to an int, restore the sign (+ or -), return result as number. | |
return parseInt(reversedStr) * Math.sign(num); |
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
/* | |
Create a function that checks if a string is a palindrome. | |
*/ | |
//Most direct solution | |
function palindrome(str) { | |
return str === str.split('').reverse().join(''); | |
} | |
//Solution that uses Array.every() helper. Note, this is inefficient as you do twice the work. |
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
/* | |
A few ways to reverse a string using JavaScript. | |
*/ | |
//Most direct solution | |
function reverse(str) { | |
return str.split('').reverse().join(''); | |
} | |
//Example using the Array.reduce helper. |
NewerOlder