
- 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
Representing seconds in HH:MM:SS in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of single characters, arr, as the first and the only argument.
The array can contain only 4 characters, they are −
- ‘N’ → stands for North direction
- ‘S’ → stands for South direction
- ‘W’ → stands for West direction
- ‘E’ → stands for East direction
Each character specifies a move of unit distance in that particular direction. And if anywhere in the array, two opposite directions, [(‘S’ and ‘N’) or (‘E’ and ‘W’)] appear adjacently, they cancel out the movement of each other. Therefore, our function is supposed to find the resulting direction of movement of the whole array.
For example, if the input to the function is −
Input
const arr = ['N', 'S', 'S', 'E', 'W', 'N', 'W'];
Output
const output = 'W';
Output Explanation
‘N’ and ‘S’ will cancel each other ‘E’ and ‘W’ will cancel each other and then finally ‘N’ and ‘S’ again cancels each other to leave behind only ‘W’.
Example
Following is the code −
const arr = ['N', 'S', 'S', 'E', 'W', 'N', 'W']; const cancelDirections = (arr = []) => { let str = arr.join(''); while(str.includes('NS') || str.includes('SN') || str.includes('EW') || str.includes('WE')){ str = str.replace('NS', ''); str = str.replace('SN', ''); str = str.replace('EW', ''); str = str.replace('WE', ''); }; return str.split(''); }; console.log(cancelDirections(arr));
Output
['W']
- Related Articles
- How to convert seconds to HH-MM-SS with JavaScript?
- How to Convert Seconds to Time (hh mm ss) or Vice Versa in Excel?
- Display Seconds in ss format (01, 02) in Java
- Display seconds with SimpleDateFormat('ss') in Java
- Display hour with SimpleDateFormat(“hh”) in Java
- What does [Ss]* mean in regex in PHP?
- Format hour in HH (00-23) format in Java
- Representing number as the power and product of primes in JavaScript
- Get current date/time in seconds - JavaScript?
- How to convert JavaScript seconds to minutes and seconds?
- Display hour in hh (01-12 in AM/PM) format in Java
- How to get current date/time in seconds in JavaScript?
- Converting seconds in years days hours and minutes in JavaScript
- Return a map representing the frequency of each data type in an array in JavaScript
- How can I get seconds since epoch in JavaScript?

Advertisements