/    Sign up×
Community /Pin to ProfileBookmark

I Need Help In JavaScript Problem

Help Plzzzz!!!!!!!!!!!!!!!!!!!…

>

A palindrome is a number or a text phrase that reads the same backward and forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611.
> ,mvcxmzxWrite a script that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long or is not a valid number, output XHTML text that displays an
> alert dialog indicating the problem to the user. Allow the user to enter a new value after dismissing the alert dialog. Continue the whole process until the user enters “stop”.
var startNumber = parseInt(12345/10000)
var endNumber = 12345%10
var remainNumber = parseIn((12345-startNumber*10000)/10)
[B]> This is not a Homework at all.[/B]
> plzz in Java script.
> Hellllllllllllllllllllllllllllp

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@jalarieMar 01.2007 — This sure looks like a homework problem to me, but I'll take your word for it:
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html lang="en-US"&gt;
&lt;head&gt;

<i> </i>&lt;title&gt;Palindrome&lt;/title&gt;

<i> </i>&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
<i> </i>&lt;meta http-equiv="Content-Script-Type" content="text/javascript" /&gt;
<i> </i>&lt;meta http-equiv="Content-Style-Type" content="text/css" /&gt;
<i> </i>&lt;meta http-equiv="Content-Language" content="en-US" /&gt;
<i> </i>&lt;meta name="Author" content="James Alarie - [email protected]" /&gt;

<i> </i>&lt;link rel="icon" href="favicon.ico" /&gt;
<i> </i>&lt;link rev="made" href="mailto:[email protected]" /&gt;

&lt;!--
Author: James Alarie
Company: University of Michigan - Flint
Address: Murchie Science Building - 207D
303 E Kearsley St
Flint MI 48502
United States of America
Telephone: +1-810-762-3394x21
Fax: +1-810-762-3687
Web Site: http://spruce.flint.umich.edu/~jalarie/
E-Mail: [email protected]
Comments: Having said that, I've probably told you more than I know.
--&gt;

<i> </i>&lt;script type="text/javascript"&gt;
<i> </i> &lt;!-- Hide this code from non-JavaScript browsers
<i> </i> function CheckIt() {
<i> </i> f1=document.forms[0]; // abbreviation
<i> </i> IVal=f1.In.value;
<i> </i> if (IVal == 'stop') {
<i> </i> return false;
<i> </i> }
<i> </i> if (IVal.match(/^[0-9]{5}$/)) {
<i> </i> D1=Math.floor(IVal/10000);
<i> </i> IVal=IVal-D1*10000;
<i> </i> D2=Math.floor(IVal/1000);
<i> </i> IVal=IVal-D2*1000
<i> </i> D3=Math.floor(IVal/100);
<i> </i> IVal=IVal-D3*100;
<i> </i> D4=Math.floor(IVal/10);
<i> </i> IVal=IVal-D4*10;
<i> </i> D5=IVal;
<i> </i> if ((D1 == D5) &amp;&amp; (D2 == D4)) {
<i> </i> alert('That is a palindrome.');
<i> </i> } else {
<i> </i> alert('That is NOT a palindrome.');
<i> </i> }
<i> </i> return false;
<i> </i> } else {
<i> </i> alert('That is not a 5-digit number');
<i> </i> return false;
<i> </i> }
<i> </i> return false;
<i> </i> } // CheckIt
<i> </i> // End hiding --&gt;
<i> </i>&lt;/script&gt;

&lt;/head&gt;

&lt;body bgcolor="#ffffee" text="black" link="blue" vlink="#800088" alink="red"&gt;
&lt;!-- Page Header --&gt;
&lt;div class="header"&gt;
&lt;div align="center"&gt;&lt;h1&gt;Palindrome&lt;/h1&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;/div&gt;

&lt;!-- Content --&gt;
&lt;div class="content"&gt;
&lt;br /&gt;
&lt;noscript&gt;
&lt;font color="red"&gt;
You must have javascript to make use of this page.
&lt;/font&gt;
&lt;/noscript&gt;

<i> </i> &lt;div align="center"&gt;
<i> </i> &lt;form onsubmit="return CheckIt()"&gt;
<i> </i> Enter your 5-digit number:&amp;nbsp;
<i> </i> &lt;input type="text" name="In" id="In" /&gt;
<i> </i> &lt;br /&gt;
<i> </i> &lt;input type="submit" value="Submit" title="Submit" /&gt;&amp;nbsp;
<i> </i> &lt;input type="reset" value="Reset" title="Reset" /&gt;
<i> </i> &lt;/form&gt;

<i> </i> &lt;/div&gt;

<i> </i>&lt;/div&gt;

&lt;!-- Page Footer --&gt;
&lt;div class="footer"&gt;
&lt;br clear="all" /&gt;&lt;hr /&gt;
Written on March 1, 2007, by:&amp;nbsp;
&lt;a href="mailto:[email protected]"&gt;James Alarie&lt;/a&gt;
&lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;

Copy linkTweet thisAlerts:
@konithomimoMar 01.2007 — &lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
function palindrome(){
var s = window.prompt('Please enter in a 5 digit number','');

while(s &amp;&amp; s.length!=5)
s = window.prompt('The number must be 5 digits long only.','');

if(s){
if((s.substring(0,1)==s.substring(4,5))&amp;&amp;(s.substring(1,2)==s.substring(3,4)))
alert('It is a palindrome');
else
alert('It is not a palindrome');
palindrome();
}

return;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="palindrome()"
&lt;/body&gt;
&lt;/html&gt;


Just add in your XHTML, and doctype. Of course, you can do it without substrings. You can use a loop to check if the reverse is the same as the regular string, or you can use split to split it at each character and then reverse it. Or you can do a long drawn out process of using division and remainders to check it.
Copy linkTweet thisAlerts:
@ToonMarinerMar 01.2007 — If you wanted to check any length string/number then try this...

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<title>HomePage</title>
<script type="text/javascript">
function palindrome()
{
var l = document.getElementById('data').value;
if (l.length < 2)
{
alert('Please enter more than 1 character.')
}
mid = l.length / 2;
m = l.length % 2;
if (m == 1)
{
s1 = l.substring(0,Math.ceil(mid));
te = l.substring(Math.floor(mid),l.length);
}
else
{
s1 = l.substring(0,mid);
te = l.substring(mid,l.length);
}
s2 = '';
for (i = 0; i <= te.length; i++) {
s2 = te.charAt(i) + s2;
}

if (s1 == s2)
{
alert(l + ' is a palindrome.');
}
else
{
alert(l + ' is not a palindrome.');
}
return false;

}
</script>
</head>
<body>
<div id="content">
<form onsubmit="return palindrome();">
<label>Enter string:<input type="text" name="data" id="data" /></label>
<input type="submit" name="submit" value="Check" />
</form>
</div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@konithomimoMar 01.2007 — or:
function palindrome(){
var s = window.prompt('Please enter a number','');

if(s){
var e = s.length-1;
var n = 0;
for(var i=0;i&lt;s.length;i++)
{
e = s.length-i;
if (s.substring(i,i+1)!=s.substring(e-1,e))
n++;
}
if(n&gt;0)
alert('It is not a palindrome');
else
alert('It is a palindrome');

palindrome();
}

return;
}


That will work for any number/string.
Copy linkTweet thisAlerts:
@Arty_EffemMar 01.2007 — 
[B]> This is not a Homework at all.[/B][/QUOTE]
Well you've just quoted well known homework assignment, so why do you need the answer? Do you need a working example to teach a class?[CODE]<script type='text/javascript'>
var inp, quit=false;

do{
while(!(inp=prompt('Enter a 5-digit number or "Stop"','')) || (!/^d{5}$/.test(inp) && !(quit=/stop/i.test(inp))))
;

if(!quit)
alert(inp+ ' is ' + (isPalindrome(inp)?"":"not")+' a palindrome.');

}while(!quit);

function isPalindrome(s)
{
str=s.toLowerCase();

for(var i=0, halfLen=Math.round(str.length/2); i<halfLen && str.charAt(i)==str.charAt(str.length-1-i); i++)
;
return i==halfLen;
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@ToonMarinerMar 01.2007 — OR... ?

[CODE]<script type="text/javascript">
function palindrome()
{
var s = document.getElementById('data').value;
if (s.length < 2)
{
alert('Please enter more than 1 character.')
}

var s2 = '';
for (i = 0; i <= ls.length; i++) {
s2 = s.charAt(i) + s2;
}

if (s == s2)
{
s += ' is a palindrome.');
}
else
{
s += ' is not a palindrome.');
}
alert(s);
return false;

}
</script>
[/CODE]


I reckon that is about as elegant as I could get it....
Copy linkTweet thisAlerts:
@konithomimoMar 01.2007 — OR... ?

[CODE]<script type="text/javascript">
function palindrome()
{
var s = document.getElementById('data').value;
if (s.length < 2)
{
alert('Please enter more than 1 character.')
}

var s2 = '';
for (i = 0; i <= ls.length; i++) {
s2 = s.charAt(i) + s2;
}

if (s == s2)
{
s += ' is a palindrome.');
}
else
{
s += ' is not a palindrome.');
}
alert(s);
return false;

}
</script>
[/CODE]


I reckon that is about as elegant as I could get it....[/QUOTE]

Yep, that is pretty concise. It is a good example of what I mentioned you could do in my first post:

"You can use a loop to check if the reverse is the same as the regular string"
Copy linkTweet thisAlerts:
@konithomimoMar 01.2007 — And just so the OP doesnt get confused, charAt gets a single character, and yes I could have used it in my code as well, but I prefer substring(), or substr() in PHP,since it is more versatile. substring() gets a character range instead of just a single character.
Copy linkTweet thisAlerts:
@Arty_EffemMar 01.2007 — OR... ?

[CODE]<script type="text/javascript">
function palindrome()
{
var s = document.getElementById('data').value;
if (s.length < 2)
{
alert('Please enter more than 1 character.')
}

var s2 = '';
for (i = 0; i <= ls.length; i++) {
s2 = s.charAt(i) + s2;
}

if (s == s2)
{
s += ' is a palindrome.');
}
else
{
s += ' is not a palindrome.');
}
alert(s);
return false;

}
</script>
[/CODE]


I reckon that is about as elegant as I could get it....[/QUOTE]
Not with 3 typos, but why build a new string when you can scan the original from both ends to the middle?
×

Success!

Help @marylove2007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...