November 27, 201411 yr Hello. Have some problems here. Having just made this in java. The numbers range from 1- 10. But then I'll have those from 10-1. Someone who can explain to me how I do it? <html> <body> <script type="text/javascript"> var i=0 for (i=1;i<=10;i++) { if (i==3){continue} document.write( + i) document.write("<br />") } </script> </body> </html
November 28, 201411 yr Agree with Nock, Java and JavaScript are not the same, there are only two similarities: the names (which originially was a marketing ploy to exploit Java's, at the time sucesss) and some of the syntax, which both inherit from C based languages. The similarities end there. Contrary to popular beleif JavaScript is NOT a subset of Java. It is in it's own right a fully fledged object oriented language, it has some bad parts but it also has brillance thrown in also, just don't go thinking it's only a scripting language. it is not. A few other points (I know you're new to this but they are good to know) Don't use inline JavaScript, use the script tag in your html to link to an external JavaScript file. Secondly don't use document.write, if this were to execute after the page had loaded it would write over everything & you'd lose all your content. instead: var content = 'some text', el = document.querySelector('#elementId'); el.innerHTML = content; Where #elementId equals an element in your html document given that id. You don't need a continue statement in a for loop, I have no idea why that example has it, the output is as nock says, the loop will increment the variable I by one each time and output the value. The loop code can be improved and this is how I'd do it: var i, content = '', el = document.querySelector('#elementId'); for (i=1; i<=10; i++){ content += i + '<br>'; } el.innerHTML = content; Another thing to take not of, what I'm doing here is gathering up all the values of I plus the line break tags into one big string. rather than appending to the DOM on each itteration of the loop I append all of it once the loop has exited. DOM manipulation is one of JavaScripts biggest bottlenecks and you should always try to minimise the number of repaints you force it to do, here there is just a single repaint. If you append inside the loop each time you get 10 repaints. The latter is a very inefficient way of doing things. I hope this post is helpful and doesn't add to your confusion. I recommend you find a new place to learn JavaScript because the exmples you seem to be getting are terrible. CodeSchool is a very good place to start. As are Douglas crockford videos on youtube, but they can get advanced quickly. Edited November 28, 201411 yr by rbrtsmith
Create an account or sign in to comment