samedi 29 août 2020

Random Testomonial Selection JS

I am working on randomising reviews/testomonials in a website i'm working on. I am using JS to randomise the testomonials that are on the index page onLoad. In my code, I am using an array to store the testomonials and then when the page loads I want it to just randomly select 3 reviews from the array and write them to "review-1", "review-2" and "review-3" respectively.

The issue I am having with my code is that idk the best way to select 3 different reviews without it repeating the same one twice.

 var reviews = [
    "Thank you Anne for fitting me in yesterday when you realised I was desperate to get the house cleaned before the blinds and curtains were fitted. Marie and Michaela did a great job, leaving it sparkling clean. I will certainly recommend you to anyone who needs a cleaner. That you are so approachable, helpful and friendly is a bonus. - <strong>Rosemary OBoyle</strong>",
    "Great job on all the awkward hate to do Jobs! Came home from my holidays and my house was sparkling, highly recommended!! - <strong>Lynne Gardiner</strong>",
    "Domestic Angels are angels to me, just left lovely Kelly cleaning my house in preparation for mums arrival, while I chill at hairdressers, thank you to Anne & her team, can\'t recommend them enough - <strong>Julie Magee</strong>"
]

var max = reviews.length;
var id1;
var id2;
var id3;

function getRandomReview(max) {
    id1 = Math.floor(Math.random() * Math.floor(max));
    id2 = Math.floor(Math.random() * Math.Floor(max));
    id3 = Math.floor(Math.random() * Math.Floor(max));
}

function randomJS() {
    getRandomReview(max);
    document.getElementById("review-1").innerHTML = id1;
    document.getElementById("review-2").innerHTML = id2;
    document.getElementById("review-3").innerHTML = id3;
}

Any advice and help would be appreciated. Thanks in advance




Aucun commentaire:

Enregistrer un commentaire