Quote generator

Quote generator
0.0 0

#1

Hi everyone,
I’m beginner, I need to build a generator of quotes in javascript. Please can you help me!!!
The quotes will be built randomly by assembling pieces of sentence.
Choose the number of quotations generated (from 1 to 5)
Choose between 2 types of citation generators (you will need to have 2 sets of sentences)
Once the quotes are generated, propose to generate new quotes or stop the program
I am looking to display on my html page, the random results of quotations formed of pieces of sentences, one must choose a number of quotes to generate and quotes appear.
I can display a quote but it does not work for many.

<body>

		  <div class="header"></div>		   
	
			<div id="content"></div>
	 
         <p id="submission"><input type="submit" class="submit" value="Lancer" onclick="demarrer();"/></input></p>
   
      </body>
<script>
function genererCitation(nombre){
    var citationDebut, citationMilieu, citationFin;
    var randomDebut, randomMilieu, randomFin;
    
    if (nombre == "1"){
    citationDebut = ["Le succès" , "Le bonheur" , "Le voyage"];
    citationMilieu = ["est à la portée de tous", "n'en fais pas une reverie sans passion sans force", "c'est savoir ce que l'on veut et le vouloir passionnément"];
    citationFin = ["il faut aller le chercher", "prends soin de lui dans tes rêves et alors il sera visible", "il suffit d'ouvrir les yeux et de regarder dans la bonne direction"];
    } 
    else if (nombre == "2"){
      citationDebut = ["Mettez votre attention de tous les jours, sur les choses que vous aimez", "L'épanouissement de l'être est la plus grande des réussites, réalisez", "Foncez, Réalisez"];
      citationMilieu = ["les choses qui vous excitent", "vos rêves", "toutes vos passions"];
      citationFin = ["et alors, vous serez sur le chemin d'une vie agréable ", "et la vie sera plus facile ", "et cela vous mettra de bonne humeur"];
    } 
    else {
   alert("Merci de choisir 1 ou 2");
    }
    
    randomDebut = citationDebut[Math.floor(Math.random() * citationDebut.length)];
    randomMilieu = citationMilieu[Math.floor(Math.random() * citationMilieu.length)];
    randomFin = citationFin[Math.floor(Math.random() * citationFin.length)];
   
    var resultat = "\"" + randomDebut + " "+ randomMilieu + ", "+ randomFin +"\!" + "\"";
    console.log(resultat);
	
	document.getElementById("content").innerHTML = resultat;
	 
         }
  
  function demarrer(){
	var choix = Number(prompt("
Type 1 to choose generator 1 or type 2 for generator 2"));
    var nombre = Number(prompt("
How many quotes do you want to generate?"));
    for(i = 0; i <nombre; i++){
      genererCitation(choix);
    }
  }
<script>

#2

Hi emma,

Actually in this challenge, you shouldn’t build your own quotes. You have to look for an API that you’ll call to get random quotes. You can call it using XMLHttpRequest or using JQuery $.getJSON method.

You just need to read the documentation :slight_smile:

C’est facile, je te souhaite bon courage :wink:


#3

Thank you Hacene for the clue !!:wink:


#4

@emma22 There is nothing wrong with building your own quotes for this project. There is no requirement to use an API for the quotes. You can approach this project either way. You will get plenty of experience on other projects which will require you to use an API.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.