Skip to content

number palindrome by converting to string and simple iteration #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/problems/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
we get abba. So this is a palindrome
*/

palindrome = string => {
palindrome = (string) => {
let reversedString = string
.split('')
.reduce((reversed, character) => character + reversed, '');
Expand All @@ -13,7 +13,7 @@ palindrome = string => {

// palindrome of a string using every array helper method

palindromeUsingEvery = string => {
palindromeUsingEvery = (string) => {
return string
.split('')
.every(
Expand All @@ -23,7 +23,7 @@ palindromeUsingEvery = string => {

// palindrome for a given number using reduce method

palindromeForANumber = number => {
palindromeForANumber = (number) => {
let reversedNumber = number
.toString()
.split('')
Expand All @@ -33,7 +33,7 @@ palindromeForANumber = number => {

// palindrome without array helper method

palindromeForANumberWithoutReduce = number => {
palindromeForANumberWithoutReduce = (number) => {
let num = number;
let arr = [];
while (num > 0) {
Expand All @@ -43,3 +43,15 @@ palindromeForANumberWithoutReduce = number => {
}
return +arr.join('') === number;
};

//palindrome of a number by converting to string and simple iteration
const numberPalindromeWithString = (num) => {
var s = num.toString();
var reverse = '';
var i;
for (i = s.length; i >= 0; i--) {
reverse = reverse + s.charAt(i);
}
if (reverse === s) return s + ' is a plindrome';
else return s + ' is not a palindrome';
};