Skip to main content
Bounty Awarded with 50 reputation awarded by CommunityBot
update snippet to match original code design
Source Link
  • The old-new and new-old modes move through the arrays from top to bottomtop-to-bottom and bottom to topbottom-to-top, respectively.
  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • determine which radio buttons are selected via RadioNodeList.value

  • use an <output> element for the display of the thing.

  • use a class to encapsulate the indexes, along with methods to get an item based on the mode

const optionsclass =ThingList {
  video:constructor(items) {
    options: [this.items //VIDEO= ARRAYitems;
    this.randomUnused = 'video1',[...items];
    this.forwardIndex = 'video2',0;
    this.reverseIndex = 'video3',
items.length - 1;
  }
  'video4',forwardItem() {
    return this.items[this.forwardIndex++ 'video5',% (this.items.length)];
  }
  ],randomItem() {
    currentIndex:if -1(!this.randomUnused.length) {
  },
  audio: { this.randomUnused.push(...this.items);
    options:}
 [ //AUDIO ARRAY
 const index = Math.floor(Math.random() * 'audio1',this.randomUnused.length)
     return 'audio2'this.randomUnused.splice(index,
  1);
  }
  reverseItem() 'audio3',{
    if (this.reverseIndex 'audio4',< 0) {
      'audio5',this.reverseIndex = this.items.length - 1;
    ],}
    currentIndex:return this.items[this.reverseIndex-1-];
  },
}
const options photo:= {
    optionsvideo: new ThingList([ //PHOTOVIDEO ARRAY
    'video1',
  'photo1'  'video2',
    'video3',
    'photo2''video4',
    'video5',
  'photo3']),
  audio: new ThingList([ //AUDIO 'photo4',ARRAY
      'photo5''audio1',
    ]'audio2',
    currentIndex: -1'audio3',
  }  'audio4',
  text: { 'audio5',
  ]),
  optionsphoto: new ThingList([ //TEXTPHOTO ARRAY
    'photo1',
  'text1'  'photo2',
    'photo3',
  'text2'  'photo4',
    'photo5',
  'text3']),
  text: new ThingList([ //TEXT 'text4',ARRAY
    'text1',
  'text5'  'text2',
    ]'text3',
    currentIndex: -1'text4',
  }  'text5',
  ])
}
const output = document.getElementsByTagName('output')[0];
//GENERATOR FUNCTION
function newThing() {
  if (!(document.forms.thingSelection.type.value in options)) {
    return false;
  }
  const list = options[document.forms.thingSelection.type.value];
  switchconst (method = document.forms.thingSelection.mode.value) {
    case+ 'random':'Item';
     const list.currentIndexitem = Math.floor(Math.random() * (list.options.length));
      break;
    case 'asc':
      if list[method](list.currentIndex < 1) {
        list.currentIndex = list.options.length;
      }
      list.currentIndex--;
      break;
    case 'desc':
      ++list.currentIndex;
      break;
  }
  output.innerHTML = list.options[list.currentIndex % (list.options.length)];item;
}
document.getElementsByTagName('button')[0].addEventListener('click', newThing)
.center {
  text-align: center;
} 

.right {
  text-align: right;
}
<div class="center"><output></output></div>

<div class="center">
  <button>New Thing</button>
</div>

<form name="thingSelection">
  <label><input type="radio" name="mode" value="random" />&nbsp;Random</label>
  <br /><label><input type="radio" name="mode" value="desc"value="forward" />&nbsp;Old&nbsp;-&nbsp;New</label>
  <br /><label><input type="radio" name="mode" value="asc"value="reverse" />&nbsp;New&nbsp;-&nbsp;Old</label>
  <div class="right">
    <label>Video&nbsp;<input type="radio" name="type" value="video" /></label><br />
    <label>Audio&nbsp;<input type="radio" name="type" value="audio" /></label><br />
    <label>Photo&nbsp;<input type="radio" name="type" value="photo" /></label><br />
    <label>Text&nbsp;<input type="radio" name="type" value="text" /></label>
  </div>
</form>
  • The old-new and new-old modes move through the arrays from top to bottom and bottom to top, respectively.
  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • determine which radio buttons are selected via RadioNodeList.value

  • use an <output> element for the display of the thing.

const options = {
  video: {
    options: [ //VIDEO ARRAY
      'video1',
      'video2',
      'video3',
      'video4',
      'video5',
    ],
    currentIndex: -1
  },
  audio: {
    options: [ //AUDIO ARRAY
      'audio1',
      'audio2',
       'audio3',
      'audio4',
      'audio5',
    ],
    currentIndex: -1
  },
  photo: {
    options: [ //PHOTO ARRAY
      'photo1',
      'photo2',
      'photo3',
      'photo4',
      'photo5',
    ],
    currentIndex: -1
  },
  text: {
    options: [ //TEXT ARRAY
      'text1',
      'text2',
      'text3',
      'text4',
      'text5',
    ],
    currentIndex: -1
  },
}
const output = document.getElementsByTagName('output')[0];
//GENERATOR FUNCTION
function newThing() {
  if (!(document.forms.thingSelection.type.value in options)) {
    return false;
  }
  const list = options[document.forms.thingSelection.type.value];
  switch (document.forms.thingSelection.mode.value) {
    case 'random':
      list.currentIndex = Math.floor(Math.random() * (list.options.length));
      break;
    case 'asc':
      if (list.currentIndex < 1) {
        list.currentIndex = list.options.length;
      }
      list.currentIndex--;
      break;
    case 'desc':
      ++list.currentIndex;
      break;
  }
  output.innerHTML = list.options[list.currentIndex % (list.options.length)];
}
document.getElementsByTagName('button')[0].addEventListener('click', newThing)
.center {
  text-align: center;
}
.right {
  text-align: right;
}
<div class="center"><output></output></div>

<div class="center">
  <button>New Thing</button>
</div>

<form name="thingSelection">
  <label><input type="radio" name="mode" value="random" />&nbsp;Random</label>
  <br /><label><input type="radio" name="mode" value="desc" />&nbsp;Old&nbsp;-&nbsp;New</label>
  <br /><label><input type="radio" name="mode" value="asc" />&nbsp;New&nbsp;-&nbsp;Old</label>
  <div class="right">
    <label>Video&nbsp;<input type="radio" name="type" value="video" /></label><br />
    <label>Audio&nbsp;<input type="radio" name="type" value="audio" /></label><br />
    <label>Photo&nbsp;<input type="radio" name="type" value="photo" /></label><br />
    <label>Text&nbsp;<input type="radio" name="type" value="text" /></label>
  </div>
</form>
  • The old-new and new-old modes move through the arrays from top-to-bottom and bottom-to-top, respectively.
  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • determine which radio buttons are selected via RadioNodeList.value

  • use an <output> element for the display of the thing.

  • use a class to encapsulate the indexes, along with methods to get an item based on the mode

class ThingList {
  constructor(items) {
    this.items = items;
    this.randomUnused = [...items];
    this.forwardIndex = 0;
    this.reverseIndex = items.length - 1;
  }
  forwardItem() {
    return this.items[this.forwardIndex++ % (this.items.length)];
  }
  randomItem() {
    if (!this.randomUnused.length) {
      this.randomUnused.push(...this.items);
    }
    const index = Math.floor(Math.random() * this.randomUnused.length)
    return this.randomUnused.splice(index, 1);
  }
  reverseItem() {
    if (this.reverseIndex < 0) {
      this.reverseIndex = this.items.length - 1;
    }
    return this.items[this.reverseIndex--];
  }
}
const options = {
  video: new ThingList([ //VIDEO ARRAY
    'video1',
    'video2',
    'video3',
    'video4',
    'video5',
  ]),
  audio: new ThingList([ //AUDIO ARRAY
    'audio1',
    'audio2',
    'audio3',
    'audio4',
    'audio5',
  ]),
  photo: new ThingList([ //PHOTO ARRAY
    'photo1',
    'photo2',
    'photo3',
    'photo4',
    'photo5',
  ]),
  text: new ThingList([ //TEXT ARRAY
    'text1',
    'text2',
    'text3',
    'text4',
    'text5',
  ])
}
const output = document.getElementsByTagName('output')[0];
//GENERATOR FUNCTION
function newThing() {
  if (!(document.forms.thingSelection.type.value in options)) {
    return false;
  }
  const list = options[document.forms.thingSelection.type.value];
  const method = document.forms.thingSelection.mode.value + 'Item';
  const item = list[method]();
  output.innerHTML = item;
}
document.getElementsByTagName('button')[0].addEventListener('click', newThing)
.center {
  text-align: center;
} 

.right {
  text-align: right;
}
<div class="center"><output></output></div>

<div class="center">
  <button>New Thing</button>
</div>

<form name="thingSelection">
  <label><input type="radio" name="mode" value="random" />&nbsp;Random</label>
  <br /><label><input type="radio" name="mode" value="forward" />&nbsp;Old&nbsp;-&nbsp;New</label>
  <br /><label><input type="radio" name="mode" value="reverse" />&nbsp;New&nbsp;-&nbsp;Old</label>
  <div class="right">
    <label>Video&nbsp;<input type="radio" name="type" value="video" /></label><br />
    <label>Audio&nbsp;<input type="radio" name="type" value="audio" /></label><br />
    <label>Photo&nbsp;<input type="radio" name="type" value="photo" /></label><br />
    <label>Text&nbsp;<input type="radio" name="type" value="text" /></label>
  </div>
</form>
added 144 characters in body
Source Link
  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • determine which radio buttons are selected via RadioNodeList.value

  • use an <output> element for the display of the thing.

  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • put options into an object that can contain the current index, and make the object a property of an object where the property name (i.e. key) is the type of thing - e.g.

      const options = {
        video: {
          options: [ //VIDEO ARRAY
            'video1',
            'video2',
            'video3',
            'video4',
            'video5',
          ],
          currentIndex: -1
        },
        audio: {
          options: [ //AUDIO ARRAY
            'audio1',
            'audio2',
            'audio3',
            'audio4',
            'audio5',
          ],
          currentIndex: -1
        },
        photo: {
          options: [ //PHOTO ARRAY
            'photo1',
            'photo2',
            'photo3',
            'photo4',
            'photo5',
          ],
          currentIndex: -1
        },
        text: {
          options: [ //TEXT ARRAY
            'text1',
            'text2',
            'text3',
            'text4',
            'text5',
          ],
          currentIndex: -1
        },
      }
    
  • instead of using id attributes for the radio buttons, just give them value attributes

  • reference the form elements via document.forms.elements

  • determine which radio buttons are selected via RadioNodeList.value

  • use an <output> element for the display of the thing.

added 153 characters in body
Source Link

The code uses some features specific to Ecmascript 2015ECMAScript-2015 (A.K.A. ES-6) like the spread syntax. With ES6 variables it is advisable to use const and let to limit the scope of variables and avoid bugs. Note that const doesn't mean immutability, but rather that such a variable cannot be re-assigned.

The <div> elements have align="center" and align="right". That appears to be a deprecated HTML 4 attribute and no longer supported in HTML 5. This can be converted to CSS rules. Note that text-align mostly applies to text but not otherblock containers1. To center inline elements like images, video, etc. To center those, margin might be requiredthe display would need to be set to block and margin set to auto.

The code uses some features specific to Ecmascript 2015 (A.K.A. ES-6) like the spread syntax. With ES6 variables it is advisable to use const and let to limit the scope of variables. Note that const doesn't mean immutability, but rather that such a variable cannot be re-assigned.

The <div> elements have align="center" and align="right". That appears to be a deprecated HTML 4 attribute and no longer supported in HTML 5. This can be converted to CSS rules. Note that text-align mostly applies to text but not other elements like images, video, etc. To center those, margin might be required.

The code uses some features specific to ECMAScript-2015 (A.K.A. ES-6) like the spread syntax. With ES6 variables it is advisable to use const and let to limit the scope of variables and avoid bugs. Note that const doesn't mean immutability, but rather that such a variable cannot be re-assigned.

The <div> elements have align="center" and align="right". That appears to be a deprecated HTML 4 attribute and no longer supported in HTML 5. This can be converted to CSS rules. Note that text-align applies to block containers1. To center inline elements like images, video, etc. the display would need to be set to block and margin set to auto.

Post Undeleted by Sᴀᴍ Onᴇᴌᴀ
Post Deleted by Sᴀᴍ Onᴇᴌᴀ
Source Link
Loading