string1 = prompt("String 1?");
string2 = prompt("String 2?");
if (string1.length == string2.length)
alert (string1 + " and " + string2 + " : are identical in size.")
else
alert (string1 + " and " + string2 + " : not identical in size.")
for(i=0; i<string1.length; i++)
{
for(j=0; j<string2.length; j++)
{
if (string1.charAt[i] == string2.charAt[i])
alert (string1.charAt[i] + " and " + string2.charAt[j] + " : are identical values.")
else
alert (string1.charAt[i] + " and " + string2.charAt[j] + " : are non-identical values.")
}
};
The second part of the code keeps returning “undefined” and “undefined”. I’m trying to compare both strings to see if they hold identical values. Would appreciate any help!
You are using the wrong index when you compare the characters. This:
should be:
Besides using parantheses, as Rob W pointed out, you should use the variable
jto access the character instring2.I’m not sure why you are comparing each character in one string with every character in the other string, though… If you actually want to compare each character to the corresponding character in the other string, then you should not have nested loops but a single loop.