Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

looping letters in javascript


ranma's Avatar
Member
0 0

how do you loop letters in javascript?


ghost's Avatar
0 0

What do you mean exactly?

Like:

<script>
    for( i = 0; i <= 10; i++ ) 
        document.write('a');
</script>```

?

ghost's Avatar
0 0

doesnt that write i 10 times? i think he means without the increment


ghost's Avatar
0 0

I'm not quite sure, but I think he wants to loop through different letters. There would be a couple of ways to do this, one would be to use a String.fromCharCode(int) function in a for loop.

String.fromCharCode(i) returns the character of the ascii value of i;

For example, String.fromCharCode(65) returns a capital A.

Stick this in a for loop, like so:

for (x = 65; x <= 81; x++)
{
document.write(String.fromCharCode(x));
}
</script>```

This would print every character from capital A to capital Q.

Another way to do it would be to make a string of all the characters you wish to loop through, then loop through every character in the String.charAt(int) method. This returns the character that is number int in the string, for example

```markup<script>
var chocka = "absdfty152634k";
for (i = 0; i < chocka.length; i++)
{
document.write(chocka.charAt(i));
}
</script>```

This loops through every character in the string Chocka and prints them.