I am making a user testimonial for my website. I am displaying 5 user testimonials on the homepage and these get pulled @random
from an array when the user loads the webpage. After the script takes 5 testimonials at random, it injects that into my HTML file.
This works all great, but I also want each value to be checked to make sure this value is not pulled already for one of the other 4 testimonials.
Then if 2 of the values are the same i would like the script to pull a other value of the array. Then it once again needs to check if the value that was pulled is not used in a other testimonial.
I am trying to find a way to do this as efficient as possible. I know how to do this if I am only pulling 2 values, but right now I am pulling 5 values and each needs to be checked with any other value that was pulled.
var user = [
{
name: 'Tom Levis',
body: 'Toms text'
},
{
name: 'Some Random Guy',
body: 'Some guys text'
},
{
name: 'Peter',
body: 'Peters text'
}
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
Eventually what should happen is that there are 5 different values pulled from the array. None of the results should contain the same values.