
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Calculating time taken to type words in JavaScript
Suppose we have a keyword, which instead of the traditional qwerty type key mapping, maps keys simply according to the english alphabetic order i.e., abcde...
Before we dive into the problem, we have to make the following two assumptions −
Currently our fingertip is placed at index 0, i.e., the key 'a
The time taken to move from one key to some other is the absolute difference of their index, for example the time taken to move from 'a' to 'k' will be |0 - 10| = 10
We are required to write a JavaScript function that takes in a string of english lowercase alphabets and calculates and returns the time we will be required to type the string.
For example −
If the input string is −
const str = 'dab';
Output
const output = 7;
because the movements that took place are −
'a' -> 'd' = 3 'd' -> 'a' = 3 'a' -> 'b' = 1
Example
The code for this will be −
const str = 'dab'; const findTimeTaken = (str = '') => { let timeSpent = 0; const keyboard = 'abcdefghijklmnopqrstuvwxyz'; let curr = 'a'; for(let i = 0; i < str.length; i++){ const el = str[i]; const fromIndex = keyboard.indexOf(curr); const toIndex = keyboard.indexOf(el); const time = Math.abs(fromIndex - toIndex); curr = el; timeSpent += time; }; return timeSpent; }; console.log(findTimeTaken(str));
Output
And the output in the console will be −
7
- Related Articles
- Problem: Time taken by tomatoes to rot in JavaScript
- Calculating excluded average - JavaScript
- Calculating factorial by recursion in JavaScript
- Calculating Josephus Permutations efficiently in JavaScript
- Joining two strings with two words at a time - JavaScript
- Calculating average of an array in JavaScript
- Euclidean Algorithm for calculating GCD in JavaScript
- Calculating median of an array in JavaScript
- Calculating h index of a citation in JavaScript
- Calculating a number from its factorial in JavaScript
- Calculating the weight of a string in JavaScript
- Calculating average of a sliding window in JavaScript
- Calculating the LCM of multiple numbers in JavaScript
- Calculating resistance of n devices - JavaScript
- Calculating median of an array JavaScript
