0
How to target a single character in a string?
Example target: any third character. string: "Hello World". result: "l".
5 Respuestas
+ 5
You don't need to loop in JavaScript (or in most languages actually). There are a couple ways you can accomplish this. Using the [] operator or using the charAt() method.
let a = "hello";
a[2]
a.charAt(2)
+ 2
I never seen the at method... I remember only charAt
+ 1
I meant to type charAt KrOW . Thanks!
+ 1
Thanks Zeke Williams It really worked.
0
You will have to loop through the string to make sure your target is always on the third character.
Example:-
var i;
var name = "Alex";
for(i = 0; i < name.length, i++)
{
if(name[2])
{
document.write("The third character is " + name[2]);
}
}
I didn't test it but it should give u an idea of what you want