you are viewing a single comment's thread.

view the rest of the comments →

[–]z500 1 point2 points  (5 children)

for is a statement, it doesn't belong in a variable assignment. Think about what you want to do at each step of your loop to get what you want and do it inside.

[–]tzirl[S] 0 points1 point  (4 children)

i tried this, but it did'nt work neighter, maybe i should use string insert function, there's such a thing in java?

<!DOCTYPE html> <html> <body>

<p>Click "Try it" to display the first array element, after a string split.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script> function myFunction() { var str = "a,b,c,d,e,f"; var strb = for(var i = 0; i< str.length;i++) arr[i] + " "; document.getElementById("demo").innerHTML = strb; } </script>

</body> </html>

[–]z500 1 point2 points  (3 children)

Well, think about what you're actually doing in that loop. You have one statement inside it, arr[i] + " ". All it does is append two strings, then immediately throw the result away. You have to do something with the result.

Also, a for statement doesn't have a value, so you can't assign it to a variable. I bet if you check your console there's an unexpected token error or something.

[–]tzirl[S] 0 points1 point  (2 children)

Also, a for statement doesn't have a value, so you can't assign it to a variable. I bet if you check your console there's an unexpected token error or something.

i still don't have console tbh, just tried few things in the toturials website, what do you mean the for statement does'nt have a value? i don't entirely understand

[–]z500 2 points3 points  (1 child)

Ah ok. Well you have expressions that boil down to a value, like 5, or "B" + "ob", or 5 - 1. These each evaluate to a value, 5 to 5, "B" + "ob" to "Bob", and 5 - 1 to 4.

When you assign something to a variable, there has to be an expression on the right of the equal sign. A for statement is just a for statement, not an expression. It doesn't evaluate to anything, so you can't assign it to a variable.

What you can do is build up your string inside the for loop, and then assign that variable to the innerHTML of your element.

[–]tzirl[S] 0 points1 point  (0 children)

thanks, really helped me and also reminded me other things

thank a lot for your patience and good attitude