RandomNumerJSLength.js

    0

    0

    The code creates a map of integers to their corresponding num values. It then loops through the num values, calculating the distance between each two numbers. The code then returns the distance between each number and the original variable solution.

    var solution = function (digits, num) {
      let map = new Map();
      for (let i = 0; i < digits.length; i++) {
        map.set(digits[i], i);
      }
      let distance = 0;
      let current = 0;
      for (let i = 0; i < num.length; i++) {
        let next = map.get(num[i]);
        distance += Math.abs(current - next);
        current = next;
      }
      return distance;
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.