JavaScript || Circular Array – How To Index Into Array As If It Is Circular Using Vanilla JavaScript
The following is a module with functions which demonstrates how to index into an array as if it is circular using Vanilla JavaScript.
This function adjusts a range as circular and ‘wraps around’ the value to become in bounds.
1. Circular Array
The example below demonstrates the use of ‘Utils.circularWrap‘ to index into an array as if it is circular.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// Circular Array <script> (() => { let words = ['this', 'and', 'that']; for (let index = -9; index <= 10; ++index) { let adjusted = Utils.circularWrap(index, words.length); console.log(`Index: ${index}, Adjusted Index: ${adjusted}, Word: ${words[adjusted]}`); } })(); </script> // expected output: /* Index: -9, Adjusted Index: 0, Word: this Index: -8, Adjusted Index: 1, Word: and Index: -7, Adjusted Index: 2, Word: that Index: -6, Adjusted Index: 0, Word: this Index: -5, Adjusted Index: 1, Word: and Index: -4, Adjusted Index: 2, Word: that Index: -3, Adjusted Index: 0, Word: this Index: -2, Adjusted Index: 1, Word: and Index: -1, Adjusted Index: 2, Word: that Index: 0, Adjusted Index: 0, Word: this Index: 1, Adjusted Index: 1, Word: and Index: 2, Adjusted Index: 2, Word: that Index: 3, Adjusted Index: 0, Word: this Index: 4, Adjusted Index: 1, Word: and Index: 5, Adjusted Index: 2, Word: that Index: 6, Adjusted Index: 0, Word: this Index: 7, Adjusted Index: 1, Word: and Index: 8, Adjusted Index: 2, Word: that Index: 9, Adjusted Index: 0, Word: this Index: 10, Adjusted Index: 1, Word: and */ |
2. Utils Namespace
The following is the Utils.js Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 7, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: circularWrap * USE: Adjusts a range as circular and 'wraps around' the value to become in bounds * @param left: The current number, or the 'numerator' * @param right: The upper bound maximum range number, or the 'denominator' * @return: The adjusted value according to the denominator */ exposed.circularWrap = (left, right) => { return ((left % right) + right) % right; } (function (factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply