A Pen by Jordan Maslyn

Thumbnail
This awesome code was written by jmm, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright jmm ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  Jordan Maslyn</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/jmm/a-pen-by-jordan-maslyn-vxQxqV */
function simplify(arr) {
    // Declare empty array for the results
    var result = [];

    // Sort the source array
    arr = arr.sort();

    // Loop through each element in array
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        for (var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var num = _step.value;

            // If the result doesn't already include the element,
            // push it in!
            if (!result.includes(num)) {
                result.push(num);
            }
        }

        // We should have a simplified array
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

    return result;
}

console.log(simplify([0, 9, 8, 3, 3, 3, 5, 9, 5, 0]));

Comments