javascript - 找时间在一张图中写一个数字

标签 javascript time timer calculation milliseconds

编写一个函数来计算在javaScript中用一根手指键入数字所需的毫秒数

我试图解决这个问题,但我不知道如何解决这个问题。

最佳答案

如果我很好地理解评论,这里就是答案

 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;
    }

示例:输入:digits = "0123456789", num = "201"输出:4

关于javascript - 找时间在一张图中写一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71570868/

相关文章:

php 日期和时间按时间顺序排序

Android - 在计时器内重复运行线程

javascript - 匹配 ini 值的正则表达式

javascript - 如何在加载我的网站主体之前预加载我所有的 javascript 文件?

c - 加速 C 中的 hexadoku(16 x 16,a - p)

java - 为什么使用带有 YYYY 模式的 DateTimeFormatter 格式化日期会给出错误的年份?

java - 如何设置计时器来运行函数中的另一个 Activity ?安卓应用程序

javascript - 如何设计一个每秒都需要变化的 reducer ?

javascript - Eclipse Mars - 一个特定文件不会在比较编辑器中打开?

javascript - 面向对象 JS : Is there point using prototype property in the base object?