How do I get multiple quotes to appear from my quote generator?

geebrand

Can anyone have a look and point me in the right direction with my quote generator? When I developed it in JS Bin and put the output to the console log it works fine - when you ask for 5 quotes you get five quotes etc., but now I've put into an html page I only get 1 quote regardless of how many quotes the user chooses. Thanks.

var generatorA = {
  quoteA: ['The big bad wolf', 'Little Red Riding Hood', 'Pinnochio', 'The three little pigs',
    'Prince charming', 'The gingerbread man', 'The three blind mice',
    'The sleeping beauty', 'Rapunzel', 'The seven dwarves', 'The princess'
  ],
  quoteB: ['kissed', 'cast a spell', 'swam', 'flew', 'whistled', 'sang', 'climbed', 'shouted', 'slept', 'ate', 'ran', 'enchanted'],
  quoteC: ['in the tower', 'through the forest', 'up the river',
    'up the castle', 'up the clock', 'in the garden'
  ]
};

var generatorB = {
  quoteA: ['Dracula', 'The ghost', 'Frankenstein', 'A hagged old witch', 'The werewolf', 'The mummy', 'The zombie', 'The merman', 'The vampire bat', 'The demon', 'The skeleton'],
  quoteB: ['howled', 'cackled', 'flew', 'cast a spell', 'climbed', 'shouted', 'shrieked', 'terrified', 'slithered', 'sacrificed'],
  quoteC: ['in the graveyard', 'in the haunted house', 'in a cave',
    'up the castle', 'up the clock', 'in the garden', 'on the desolate moor'
  ]
};

var dictionary = {
  a: generatorA,
  b: generatorB
};

function randQuote() {
  var n = document.getElementById("userInput").value;
  for (var i = 0; i < n; i++) {
    if (document.getElementById("quoteChoice").value === 'a') {
      sub = dictionary.a;
    } else if (document.getElementById("quoteChoice").value === 'b') {
      sub = dictionary.b;
    }
    var quote = "Here is your quote: " + sub.quoteA[Math.floor(Math.random() * sub.quoteA.length)] + ' ' +
      sub.quoteB[Math.floor(Math.random() * sub.quoteB.length)] + ' ' +
      sub.quoteC[Math.floor(Math.random() * sub.quoteC.length)] + '!';
    return quote;
  }
}

function printQuote() {
  var pTag = document.getElementById("demo");
  pTag.innerText = randQuote();
}

printQuote();
@keyframes example {
  from {
    background-color: grey;
  }
  to {
    background-color: black;
  }
}

p,
h1 {
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

.click {
  color: white;
  display: inline-block;
  padding: 5px;
  margin: 10px;
  overflow: auto;
  line-height: 1px;
  background-color: grey;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}
<div style="text-align:center">
  <h1>Random Quote Generator</h1>
  <div>
    <p>Please choose your quote generator. Choose 'a' for fairytale/nursery rhymes or 'b' for horror characters?</p>
    <select name="quoteChoice" id="quoteChoice" />>
    <option value="a">a</option>
    <option value="b">b</option>
    </select>
  </div>
  <div class=click>
    <p>How many quotes would you like? You can have up to five!</p>
    <form>
      Number of quotes?:
      <input name="userInput" id="userInput" type="number" min="1" max="5" />
      <input type="button" onclick="printQuote()" value="Submit" />
    </form>
  </div>
  <div>
    <p id="demo"></p>
  </div>
</div>

https://codepen.io/robgillibrand/pen/XZYQPN

D.B.

Like mentioned in comments by Patrick, the return value was inside loop... it should be on outside. Also, the quote variable should be defined on the outside as well and then each quote in loop appended... or at least that's one way to do it.

var generatorA = {
   quoteA: ['The big bad wolf','Little Red Riding Hood','Pinnochio', 'The three little pigs',
  'Prince charming', 'The gingerbread man', 'The three blind mice',
  'The sleeping beauty','Rapunzel','The seven dwarves','The princess'],
  quoteB: ['kissed','cast a spell', 'swam', 'flew', 'whistled', 'sang', 'climbed','shouted','slept','ate', 'ran', 'enchanted'],
  quoteC: ['in the tower', 'through the forest','up the river',
  'up the castle','up the clock','in the garden']};

var generatorB = {
   quoteA: ['Dracula', 'The ghost', 'Frankenstein','A hagged old witch','The werewolf','The mummy', 'The zombie', 'The merman', 'The vampire bat','The demon','The skeleton'],
  quoteB: ['howled', 'cackled', 'flew', 'cast a spell', 'climbed', 'shouted','shrieked','terrified','slithered', 'sacrificed'],
  quoteC: ['in the graveyard', 'in the haunted house','in a cave',
  'up the castle','up the clock','in the garden','on the desolate moor']};

var dictionary = {
  a: generatorA,
  b: generatorB
};

function randQuote() {
	var n = document.getElementById("userInput").value;
  var quote='';
	for (var i = 0; i < n; i++) {
		if (document.getElementById("quoteChoice").value==='a'){ 
      sub = dictionary.a;
    } else if (document.getElementById("quoteChoice").value==='b'){
      sub = dictionary.b;
    } 
    quote += sub.quoteA[Math.floor(Math.random()*sub.quoteA.length)]+' '
    +sub.quoteB[Math.floor(Math.random()*sub.quoteB.length)]+' '
    +sub.quoteC[Math.floor(Math.random()*sub.quoteC.length)]+'!'
    +'\n';
  }
      return "Here is your quote: \n"+ quote;
}

function printQuote() {
	var pTag = document.getElementById("demo");
	pTag.innerText=randQuote();
}

printQuote();
@keyframes example {
    from {background-color: grey;}
    to {background-color: black;}
}

p, h1 {
    font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

.click {
	color: white;
	display: inline-block;
	padding: 5px;
	margin: 10px;
	overflow: auto;
	line-height: 1px;
    background-color: grey;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}
<div style="text-align:center">
	<h1>Random Quote Generator</h1>
		<div>
			<p>Please choose your quote generator. Choose 'a' for fairytale/nursery rhymes or 'b' for horror characters?</p>
		<select name="quoteChoice" id="quoteChoice"/>>
			<option value="a">a</option>
			<option value="b">b</option>  
		</select>		
	</div>	
	<div class=click>
		<p>How many quotes would you like? You can have up to five!</p>
			<form>
				Number of quotes?: 
				<input name="userInput" id="userInput" type="number" min="1" max="5"/>
				<input type="button" onclick="printQuote()" value="Submit"/>
			</form>	
	</div>		
	<div>	
		<p id="demo"></p>
	</div>	
</div> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get my Dock to appear on the left in Ubuntu 20.10?

From Dev

How do I get this unicode character to appear on my website?

From Dev

How do i get my repeater results to appear line by line

From Dev

How do I get my app to appear in the Applications list?

From Dev

How do I get excact single quotes words from the sentence?

From Dev

How do I get my application to appear on the center of my second monitor, not my main?

From Dev

I am confused with how to quote quotes

From Dev

When creating my own custom yeoman generator, how do I use a variable from the prompt in my Gruntfile?

From Dev

how to extract a quote from a quote generator in Vb.net

From Java

How do I get my data from my DB as a variable?

From Dev

How do I get the blue location dot to appear in my Android application?

From Dev

How do I get this HTML canvas animation to appear on top of my image?

From Dev

How do I get hidden HTML to appear only in my popover on click, not elsewhere on the page?

From Dev

How do I get the blue location dot to appear in my Android application?

From Dev

How do I get the name of the month (char) and the Year (date) to appear in my data item and column in Cognos

From Dev

How do I get a drawer to appear above the rest of my web page, so that the underlying elements are not seen and clickable?

From Dev

How do I get the proper characters to appear instead of "?"s in my jSP page?

From Dev

How do I get my menu blocks to appear over banner again?

From Dev

How do I read a tarfile from a generator?

From Dev

How do I use awk's single quotes within another single quote phrase?

From Dev

How can I get Django to return JsonResponse with no extra quotes or quote escapes?

From Dev

How do I insert random images from my drawable folder to randomly appear on the screen when loaded?

From Dev

How do I make my link list appear in order from top to bottom, not right to left?

From Dev

How do I get MKS sed to output this double quote?

From Dev

How do I get MKS sed to output this double quote?

From Dev

How do I get multiple values from ajax cfc? I keep getting obj obj in my text field

From Dev

How do I remove multiple offending characters from my string?

From Dev

How do I prevent $@ from clashing with double quote in bash?

From Dev

Using Seaborn, how do I get all the elements from a pointplot to appear above the elements of a violoinplot?

Related Related

  1. 1

    How do I get my Dock to appear on the left in Ubuntu 20.10?

  2. 2

    How do I get this unicode character to appear on my website?

  3. 3

    How do i get my repeater results to appear line by line

  4. 4

    How do I get my app to appear in the Applications list?

  5. 5

    How do I get excact single quotes words from the sentence?

  6. 6

    How do I get my application to appear on the center of my second monitor, not my main?

  7. 7

    I am confused with how to quote quotes

  8. 8

    When creating my own custom yeoman generator, how do I use a variable from the prompt in my Gruntfile?

  9. 9

    how to extract a quote from a quote generator in Vb.net

  10. 10

    How do I get my data from my DB as a variable?

  11. 11

    How do I get the blue location dot to appear in my Android application?

  12. 12

    How do I get this HTML canvas animation to appear on top of my image?

  13. 13

    How do I get hidden HTML to appear only in my popover on click, not elsewhere on the page?

  14. 14

    How do I get the blue location dot to appear in my Android application?

  15. 15

    How do I get the name of the month (char) and the Year (date) to appear in my data item and column in Cognos

  16. 16

    How do I get a drawer to appear above the rest of my web page, so that the underlying elements are not seen and clickable?

  17. 17

    How do I get the proper characters to appear instead of "?"s in my jSP page?

  18. 18

    How do I get my menu blocks to appear over banner again?

  19. 19

    How do I read a tarfile from a generator?

  20. 20

    How do I use awk's single quotes within another single quote phrase?

  21. 21

    How can I get Django to return JsonResponse with no extra quotes or quote escapes?

  22. 22

    How do I insert random images from my drawable folder to randomly appear on the screen when loaded?

  23. 23

    How do I make my link list appear in order from top to bottom, not right to left?

  24. 24

    How do I get MKS sed to output this double quote?

  25. 25

    How do I get MKS sed to output this double quote?

  26. 26

    How do I get multiple values from ajax cfc? I keep getting obj obj in my text field

  27. 27

    How do I remove multiple offending characters from my string?

  28. 28

    How do I prevent $@ from clashing with double quote in bash?

  29. 29

    Using Seaborn, how do I get all the elements from a pointplot to appear above the elements of a violoinplot?

HotTag

Archive