Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Please Help!

OpenStudy (anonymous):

1.) Make an html file. -I need just like a basic setup. 2.) How do you utilize javascript to loopsomething as many times as you want? - Set up. 3.) How to make a RandomWord() function? - Set up.

OpenStudy (anonymous):

@wio @dumbcow

OpenStudy (anonymous):

i want this all to be in a HTML page.... If anyone can teach me how, i would appreciate it :D

thomaster (thomaster):

HTML is easy to setup start a tag with <x> and end with </x> (x is the tag, like body or head) So you always start with: <html> after html tag you use head <head> in the head you setup things like page title (<title>this is the title</title> Also the metatags are in the head (search engines use meta tags) You also initiate scripts like javascript in the head. Next is the body <body> The body is the visible part in browsers Here you can use things like headings <H1>Big text</H1> (number 1 to 6) So your page is: <html> <head> <title>page title</title> <body> (page) </body> </head> </html>

thomaster (thomaster):

javascript is used by including it in <script src="source" type="text/javascript"></script>

thomaster (thomaster):

And a randomword function: function createRandomWord(length) { var consonants = 'bcdfghjklmnpqrstvwxyz', vowels = 'aeiou', rand = function(limit) { return Math.floor(Math.random()*limit); }, i, word='', length = parseInt(length,10), consonants = consonants.split(''), vowels = vowels.split(''); for (i=0;i<length/2;i++) { var randConsonant = consonants[rand(consonants.length)], randVowel = vowels[rand(vowels.length)]; word += (i===0) ? randConsonant.toUpperCase() : randConsonant; word += i*2<length-1 ? randVowel : ''; } return word; } alert( createRandomWord(10) );

OpenStudy (anonymous):

so how would i compile it into a single HTML page?

thomaster (thomaster):

<html> <head> <title>page title</title> <body> <script type="text/javascript"> function createRandomWord(length) { var consonants = 'bcdfghjklmnpqrstvwxyz', vowels = 'aeiou', rand = function(limit) { return Math.floor(Math.random()*limit); }, i, word='', length = parseInt(length,10), consonants = consonants.split(''), vowels = vowels.split(''); for (i=0;i<length/2;i++) { var randConsonant = consonants[rand(consonants.length)], randVowel = vowels[rand(vowels.length)]; word += (i===0) ? randConsonant.toUpperCase() : randConsonant; word += i*2<length-1 ? randVowel : ''; } return word; } alert( createRandomWord(10) ); </script> </body> </head> </html> This function will make a popup with a random word

thomaster (thomaster):

If you change the 10 into 15 or something th word will contain 15 letters instead of 10 :)

thomaster (thomaster):

You can also make a nice button :)) if you click it the random word will appear, which is way more fun. Include this in the body: <h1>random word generator</h1> <form> <input type="button" onClick="alert( createRandomWord(10) )" value="Give me word!!"> </form> And remove this from the script: alert( createRandomWord(10) );

thomaster (thomaster):

The popup is caused by the alert. To make text of it replace alert by document.write So: document.write( createRandomWord(10) )

OpenStudy (anonymous):

hmm well okaii, i seem to understand it better :) But how about if I was trying to use javascript to loop something?

thomaster (thomaster):

What would you like to loop? random words? :P Loops are made with for. like: for (var i=0; i<5; i++) { x=x + "The number is " + i + "<br>"; } This function does the following: -variable i is set to zero. -IF i is lower than 5, add 1 to i (i++) -next it writes on the page the current value of i followed by a br tag (enter) It will repeat as long as i is less then 5

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!