-1

I need to connect the webpage with a 'random.js' file which I hope I have already correctly done, as well as creating a function named 'GenerateSequence' that generates 3 random letters. I have inserted 3 'RandomChar' into the body but am not entirely sure where exactly they go. In the end the page needs to look like this

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
 <title>Random Letter Sequence</title>

 <script type="text/javascript" src="random.js"></script>

 <script type="text/javascript">
 function GenerateSequence()
 {

 }
</script>
</head>

<body>
<h2>Generate 3-Letter Sequence</h2>

sequence =  RandomChar('abcdefghijklmnopqrstuvwxyz') +
        RandomChar('abcdefghijklmnopqrstuvwxyz') +
        RandomChar('abcdefghijklmnopqrstuvwxyz');

<hr>
<h3>You Random 3-Letter Sequence<h3>
<div id="outputDiv"></div>
</body>
</html> 
Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    [What have you tried?](http://mattgemmell.com/what-have-you-tried/) SO is not a site where you can just ask for finished code. – Cerbrus Feb 18 '14 at 09:00
  • 1) put the script statements inside the function 2) use [Math.random](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) and 3) insert using [innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/element.innerHTML) – mplungjan Feb 18 '14 at 09:01
  • Please consider reading the [Javascript guide](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide). – georg Feb 18 '14 at 09:09
  • Well I have literally no idea what to do and even given these 3 steps above this comment I am still so lost – user3312389 Feb 18 '14 at 09:11
  • So read the documentation behind the steps I gave. Plenty of examples there – mplungjan Feb 18 '14 at 09:54

1 Answers1

0
function GenerateSequence(){
  var result = "";
  var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  for(var i=0;i<3;i++){
      result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
  }
  return result;
}
window.onload = function(){
  document.getElementById('goButton').onclick=function() {  
    document.getElementById('outputDiv').innerHTML = GenerateSequence();
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
berentrom
  • 505
  • 1
  • 5
  • 12
  • http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript <- Indeed taken from here. :-) – berentrom Feb 18 '14 at 09:06
  • So why exactly did you copypaste it? What's wrong with a simple link? – georg Feb 18 '14 at 09:06
  • 2
    To edit and provide a simple answer for the user. – berentrom Feb 18 '14 at 09:07
  • This code doesn't work though, I am getting no random sequence generated when I click the Generate button – user3312389 Feb 18 '14 at 09:16
  • No you'd still have to execute the function on a click of a button or div and output the result for the user to see. – berentrom Feb 18 '14 at 09:17
  • Well besides that this example requires me to include the statement - sequence = RandomChar('abcdefghijklmnopqrstuvwxyz') + RandomChar('abcdefghijklmnopqrstuvwxyz') + RandomChar('abcdefghijklmnopqrstuvwxyz'); into the function. How do I do this given what you have provided? – user3312389 Feb 18 '14 at 09:23
  • Execute the function and output the return in to your outputDiv. – berentrom Feb 18 '14 at 09:27
  • I read what you're writing but its like you're speaking a different language. I don't mean to annoy you but I have no idea what that means . Could you possibly explain in layman's terms? @EmeryFramboise – user3312389 Feb 18 '14 at 09:33
  • See my edit of your answer – mplungjan Feb 18 '14 at 09:53
  • Why exactly did you roll it back? I'm sincerely trying to help. – berentrom Feb 18 '14 at 09:54
  • I did not roll anything back, I just fixed your poor coding – mplungjan Feb 18 '14 at 09:55
  • Thanks guys I got it to work but rather than having the random sequence load when the page does I need it to appear when a button labeled 'Go!' in the is clicked and generates a new sequence each time clicked – user3312389 Feb 18 '14 at 09:56
  • See update. I could post my own but never mind – mplungjan Feb 18 '14 at 09:56
  • Oh yeah that's better, sorry oversaw the last part and thought it had returned to the original function. – berentrom Feb 18 '14 at 09:56
  • I have exactly what you've edited up above but it isn't working. Aptana says on the lines begining with 'window.onload' and the line after I'm missing semi colons, yet when I add them I get a syntax error? – user3312389 Feb 18 '14 at 10:08