JSFiddle - React, Tailwind, and code Playground

by csornmo

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 {
    list.splice(index, 1);
    list.push(fruit);
  }

  console.log(list);
});