0

My aim:

I am trying to create a small section on my website for testimonials.

I have 1 testimonial, when the button is clicked, the current testimonial disappears and a new random testimonial appears in the box. This works fine. But... I notice that the random selector is throwing up duplicate testimonials (Testimonial 1 is shown, the button is clicked and testimonial 1 still appears by chance)

I am trying to write a command that says: IF the new array is the same as the previous then repeat the random selection process (redo math) ELSE write (innerHTML) the new testimonial.

My problem is that I don't know the coding for the IF section (where I have scrawled "SAME AS CURRENT MESSAGE")

Also the next stage will be the "go to start of script" part (redo math)

I would really appreciate it if someone could help me out here as I am a bit clueless TBH!

Thank you in advance

function quotes() {
    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"

    //Generate a random number then print the quote from that array index
    rdmQuote = Math.floor(Math.random() * aquote.length);
    if (rdmQuote = aquote[SAME AS CURRENT MESSAGE]) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
    }
}

6 Answers 6

2

I would define the array such that it gets initialized only once. Then I would select a random entry m between 0 and n-2 (with n the array size) and exchange the m-th entry with the n-1 entry and display this entry. Thus the new selection can never select the currently displayed entry.

Sign up to request clarification or add additional context in comments.

1 Comment

This is my favorite answer since I wouldn't have thought to do it like this.
1

Store the index of the last quote and compare.

jsFiddle example

var lastQuote = -1;
function quotes() {
    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"
    //Generate a random number then print the quote from that array index
    var rdmQuote = Math.floor(Math.random() * aquote.length);   
    if (rdmQuote == lastQuote) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        lastQuote = rdmQuote;
    }
}

2 Comments

This works great! Thank you! How would I get the math to repeat once a duplicate quote is discovered?
Do you mean how would you run the function again if a duplicate quote was selected? Just change the if part to if (rdmQuote == lastQuote) { quotes() }
1

One way to do this would be to compare the content of the element with the randomly selected new content and use a loop to select a new one until they are different.

var currentContent = document.getElementById("randomtestimonial").innerHTML;
do {
  rdmQuote = aquote[Math.floor(Math.random()*aquote.length)];
} while(currentContent == rdmQuote);
document.getElementById("randomtestimonial").innerHTML = rdmQuote;

This code could be improved since it's possible that if aquote.length were ever 1 then this would be a infinite loop. But, hopefully, it's a good starting point.

3 Comments

Thank you. This still appears to be throwing up duplicates.
@MichaelWalkling that means that the call to document.getElementById("randomtestimonial").innerHTML is returning a string that's different than the content of aquote[Math.floor(Math.random()*aquote.length)].
@MichaelWalkling One suggestion (if you're not already using it) is use FireBug in FireFox to help diagnose these javascript issues. It's a wonderful tool. In this case, a call to console.log(""+ document.getElementById("randomtestimonial").innerHTML +" vs "+ aquote[Math.floor(Math.random()*aquote.length)]); inside the loop would quickly show you what's going on.
0

I would just cache the current selection.

  <!-- Hide the script from old browsers //-->
 var _cache="";
 function quotes(){
 //Define and populate the array 
 var aquote = new Array;
   // all your quotes

 //Generate a random number then print the quote from that array index
 rdmQuote = Math.floor(Math.random()*aquote.length);

 if (_cache == rdmQuote) 
     {
         alert('quote is same as current')
     }
     else
     {
       document.getElementById("randomtestimonial") .innerHTML=aquote[rdmQuote];
 }
 _cache  = rdmQuote;
 }

Comments

0

Just use a variable to store the current quote index.

var current = -1;

function quotes() {

    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"

    //Generate a random number then print the quote from that array index
    rdmQuote = Math.floor(Math.random() * aquote.length);
    if (rdmQuote == current) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        current = rdmQuote;
    }
}

2 Comments

won't current be reset to -1 whenever this function is called?
see if mine gives you what you need.
0

Your job is easier than your question title implies. You're looking to pick a random string from an array, and if it's the same as the current string, pick another.

You already know how to access this string, too, doing

rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote == document.getElementById("randomtestimonial").innerHTML]) {
    alert('quote is same as current')
} else {
    document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
}

Note, I used a double equals, not the single you did. = is assignment. == and === are equality comparison. This still leaves you with the question of what to do if it is the same, (other than alerting). You need the do/while control command.

var quote = "";
do {
    rdmQuote = Math.floor(Math.random() * aquote.length);
    quote = aquote[rdmQuote];
} while (quote == document.getElementById("randomtestimonial").innerHTML;
document.getElementById("randomtestimonial").innerHTML = quote;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.