I am writing a program for a quote generator and I need some help getting my quote to generator a set number of times depending on what the user input.

I've tried to iterate and pus the result into an array but it only pushes the same result for e.g 4 times

//Flowers quotes
'use strict';

const randomFlowersQuote = () => {

    const fragBeginning = ['These are very beautiful ', 'I like looking at ', ' Today We saw some new ', 'There a nice '];
    const fragMiddle = ['flower pots', 'hibiscuss trees', 'red roses', 'lily flowers', 'rare flowers'];

    const fragEnd = [' i would love to buy one', ' they are very nice', ' they are so breathtaking', ' in the shop'];

    //Random generated quotes

    const fragBeginningRandom = [Math.floor(Math.random() * fragBeginning.length)];
    const fragMiddelRandom = [Math.floor(Math.random() * fragMiddle.length)];
    const fragEndRandom = [Math.floor(Math.random() * fragBeginning.length)];
    const fullQuote = [fragBeginning[fragBeginningRandom] + fragMiddle[fragMiddelRandom] + fragEnd[fragEndRandom]];

    return fullQuote;
}

console.log(randomFlowersQuote());

I expect when the user enters 3 it calls the function 3 times.

1 Answers

1
Dupinder Singh On Best Solutions

Okay, I have one solution, I modified your code and try to run this, and this gives me output as you required. According to me :)

const fragBeginning = ['These are very beautiful ', 'I like looking at ', ' Today We saw some new ', 'There a nice '];
const fragMiddle = ['flower pots', 'hibiscuss trees', 'red roses', 'lily flowers', 'rare flowers'];

const fragEnd = [' i would love to buy one', ' they are very nice', ' they are so breathtaking', ' in the shop'];





    function RandomGeneratedQuotes(number)
    {   
       var quotes = [];
       for(var i = 0; i < number; i++){
            const fragBeginningRandom = [Math.floor(Math.random() * fragBeginning.length)];
            const fragMiddelRandom = [Math.floor(Math.random() * fragMiddle.length)];
            const fragEndRandom = [Math.floor(Math.random() * fragBeginning.length)];
            const fullQuote = [fragBeginning[fragBeginningRandom] + fragMiddle[fragMiddelRandom] + fragEnd[fragEndRandom]];
            quotes.push(fullQuote);
        } return quotes;
    }    

on function call output will be:

console.log(RandomGeneratedQuotes(3))
VM431:1 
(3) [Array(1), Array(1), Array(1)]
0: [" Today We saw some new rare flowers i would love to buy one"]
1: ["These are very beautiful flower pots i would love to buy one"]
2: [" Today We saw some new rare flowers in the shop"]

This should work for you flawless