<!--don't edit this file-->
<div id=question>
<span style="font-weight:bold">Question 2:</span> A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest
palindrome larger than K to output. Numbers are always displayed without leading zeros.
<P>
Example:
<br/> Number Given: 808
<br/> Output: 818
<br/> Number Given: 2133
<br/> Output: 2222
<br/>
</P>
</div>
<br/>
<p>Your Results:</p>
<div id="results2"></div>
// You may use: https://developer.mozilla.org if you need help with syntax
// https://getfirebug.com/firebug-lite-debug.js as external resource for a build-in console window
function Q2(number1) {
var returnValue = "";
var counter = 0;
var palindromeFound = false;
while (palindromeFound === false && counter < 1000) {
number1++;
console.log(`Tested Value: ${number1}`);
palindromeFound = isPalindrome(number1);
counter++;
}
returnValue = number1;
return returnValue;
}
function isPalindrome(value) {
let string = value.toString();
let characterArray = string.split('');
for (let i = 0, j = characterArray.length - 1; i < j; i++, j--) {
if (characterArray[i] !== characterArray[j]) {
return false;
}
}
return true;
}
$("#results2").append(Q2(556));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.