-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.js
More file actions
38 lines (32 loc) · 1.05 KB
/
palindrome.js
File metadata and controls
38 lines (32 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// let arr = ["D","O","R","E","M","O","N"]
// let arr = ['N','I','T','I','N']
// function palindrome(arr){
// for(let i=0; i<arr.length; i++){
// for(let j=arr.length; j<=arr.length;j--){
// if(arr[i] == arr[j]){
// console.log('It\'s a Palindrome!')
// } else {
// console.log('Not a Palindrome!')
// }
// }
// }
// }
// console.log(palindrome(arr))
let n = 1414
function Palindrome(n){
if (n < 0) return false //checking for negative numbers
let nCopy = n //storing the copy of original n because it gets changed later
let reverse = 0
while (n>0) {
remainder = n % 10 //% -> gives remainder
reverse = (10*reverse) + remainder //We are multiplying reverse by 10 because earlier, we would have divided the initial number by 10
n = Math.floor(n/10) // / -> gives quotient
}
if(reverse === nCopy){
return 'It\'s a Palindrome'
} else {
return 'Not a Palindrome!'
}
}
let result = Palindrome(n)
console.log(result)