0
  1. Make sentences from the three arrays. Firt index with first index form each array so on.. 10 sentences. (This I have figured out and managed)
  2. Use Math.random to mix the words from the arrays randomly to make new sentences, doesnt matter if a word get used two times.

Question: where do i best put the Math.random in my code?

let substantiv = ['Daddy', 'Jag', 'Hästen', 'Mamma', 'Glaset', 'Gameboy', 'Pelle','Blondie', 'Sängen', 'Bilen']
let verb = ['cyklar', 'rider', 'bakar', 'springer', 'hoppar', 'äter', 'dricker', 'går', 'läser', 'sover']
let adj = ['bra', 'dåligt', 'roligt', 'inte', 'alltid', 'på Söndag', 'aldrig', 'imorgon', 'idag', 'snabbt']

let cont = document.createElement('div')
document.body.append(cont)


for(let i=0; i<10; i++){
    const el1 = document.createElement('div')
    el1.textContent = `${substantiv[i]} `+`${verb[i]} `+`${adj[i]}`
    cont.append(el1)    
}
tocajossan
  • 41
  • 9
  • 1
    Replace the `i` indexes with 3 random indexes. – 001 Oct 13 '22 at 13:56
  • Note: seems odd to use Template literals and concatenation together. Try `\`${substantiv[i]} ${verb[i]} ${adj[i]}\`` – 001 Oct 13 '22 at 14:01

1 Answers1

0

I found this solution;

for(let i=0; i<10; i++){
    const random1 = Math.floor(Math.random()* substantiv.length)
    const random2 = Math.floor(Math.random()* verb.length)
    const random3 = Math.floor(Math.random()* adj.length)
    const el2 = document.createElement('div')
    el2.textContent = `${substantiv[random1]} ${verb[random2]} ${adj[random3]}`
    cont.append(el2)    
}
tocajossan
  • 41
  • 9