Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Thanked 0 Times in 0 Posts

    Javascript non repeating strings in array help?

    I am trying to get a new string from my array of strings everytime my button is clicked (not have the same string show up twice in a row) but it is not working. Here is my code..what am I doing wrong? Thankyou!

    function nextQuote() {
    var oldNumber;
    alert("button clicked");
    while (randomNumber == oldNumber){
    var randomNumber = Math.floor(Math.random()*quoteArray.leng…
    while (randomNumber != oldNumber)
    quoteText.innerHTML=(quoteArray[random… + '');
    authorText.innerHTML=(authorArray[rand… + '
    randomNumber = oldNumber;

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Thanked 498 Times in 492 Posts


    We don't always have time to write your code from scratch
    and it would be more helpful of you to at least include the quote array.

    And it is easier to correct you code than design it from scratch
    so it would be nice of you to also include the HTML layout you desire.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,538 Times in 2,516 Posts
    Obvious errors:-

    quoteText.innerHTML=(quoteArray[random… + '');
    authorText.innerHTML=(authorArray[rand… + '

    Use your error console to expose errors of this kind!

    You would do better to shuffle your arrays and then take them in sequence which would mean that each one was never repeated - as it is the same string may appear at first, third, fifth etc.

    <body onload = "show()">
    <input type = "button" id = "b1" value = "Show Next Quotation" onclick = "show()">
    <span id = "quote">Quotes in here</span>
    <span id = "author">Authors in here</span>
    <script type = "text/javascript">
    var q = []; // the quotations
    q[0] = "First quotation";
    q[1] = "Second quotation";
    q[2] = "Third quotation";
    q[3] = "Fourth quotation";
    q[4] = "Fifth quotation";
    q[5] = "Sixth Quotation";
    q[6] = "Seventh quotation";
    q[7] = "Eighth quotation";
    var len = q.length;
    var au = []; // the authors
    au[0] = "Author of first quotation";
    au[1] = "Author of second quotation";
    au[2]=  "Author of third quotation";
    au[3] = "Author of fourth quotation";
    au[4] = "Author of fifth quotation";
    au[5] = "Author of sixth quotation";
    au[6] = "Author of seventh quotation";
    au[7] = "Author of eighth quotation ";
    function shuffle(len) {
    var a = [], result = [];
    for (var i=0; i<len; i++)
    a[i ] = i;
    while (i>0) {
    var randy = Math.floor(Math.random()*i);
    a[randy] = a[--i];
    return result;
    var index = shuffle(len);
    alert (index);  // for testing
    var x = 0;
    function show() {
    var count = index[x];  // 0-7
    document.getElementById("quote").innerHTML = q[count];
    document.getElementById("author").innerHTML = au[count];
    if (x > len-1) {
    index = shuffle(len);  // shuffle again
    alert (index);  // for testing
    x = 0;

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-28-2013 at 09:14 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts