'Find time to write a number in one figure

write a function to calculate the number of milliseconds needed to type a number with one finger in javaScript

am try to solve this question but i don't have any idea how am solve this problem.



Solution 1:[1]

If I understand the comments well here's the answer

 function calcTime (digits, num ){
        const digits_arr = Array.from(digits);
        let last_index = 0, new_index, time =0;
        for (const n of num) {
            new_index =digits_arr.findIndex(x => x===n);
            time += Math.abs(new_index - last_index);
            last_index = new_index;
        }
        return time;
    }

example: Input: digits = "0123456789", num = "201" Output: 4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdelaziz Alsabagh