Click to See Complete Forum and Search --> : strings n stuff


whobetta
02-23-2003, 09:22 PM
hi i am trying to create a very simple routine where the application asks for input then generates statements according to the input... BUT this is for an application not an applet if that matters.. thanks here iswhat i need to accomplish...

Please enter your name: Wendy
"Wendy" contains 5 characters
the first character is a "W" and the last character is a "y"
Wendy contains at least one uppercase character
the revers of "Wendy" is "ydneW"
Wendy is not a palindrome

ok i am pretty sure how to do the first 2 with the entering the name, and how many characters it contains. but after that i am really confused... im sure you need to use a loop to check for the palindrome but not sure how to set it up. or how to reverse the name with charAt and a loop... the uppercase isnt a problem. also how would i do the first and last chars, ( charAt) again.. so if i could get some input id be very thankfull.

dabush
02-23-2003, 10:26 PM
This is it from beginning to end.

function reverse(string)
{
var newstr = '';
for (var loop = 1; loop <= string.length; loop++)
{
newstr += string.charAt(string.length-loop);
}
return newstr;
}
var name = prompt("Please enter your name.","");
alert('"'+name+'" contains '+name.length+' characters.')
alert('The first character is a "'+name.charAt(0)+'" and the last character is a "'+name.charAt(name.length-1)+'".');
if (name != name.toLowerCase()) alert('"'+name+'" contains at least one uppercase character.');
else alert('"'+name+'" contains no uppercase characters.');
alert('The reverse of "'+name+'" is "'+reverse(name)+'".');
if (name.toLowerCase() == reverse(name).toLowerCase()) alert('"'+name+'" is a palindrome.');
else alert('"'+name+'" is not a palindrome.');