JSFiddle - React, Tailwind, and code Playground

by csornmo

HTML

<button>Apple</button>
<button>Banana</button>
<button>Strawberry</button>
<button>Lime</button>
<button>Blueberry</button>

JavaScript

var list = [];

$('button').click(function() {

  var fruit = $(this).text();

    var index = list.indexOf(fruit);   
    if (index === -1) {
      if (list.length > 2) {
         list.splice(0, 1);
      }
      list.push(fruit);
    } else {
        if (index === 0) {
          list.splice(0, 1);
          list.push(fruit);
        }
    }

  console.log(list);

});