JavaScript: Insert dashes (-) between each two even digits of a specific number
JavaScript Array: Exercise-6 with Solution
Insert Dashes Between Evens
Write a JavaScript program that accepts a number as input and inserts dashes (-) between each even number. For example if you accept 025468 the output should be 0-254-6-8.
Sample Solution:
JavaScript Code:
Output:
0-254-6-8
Flowchart:

ES6 Version:
Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that accepts a number as a string and inserts dashes between every two consecutive even digits.
- Write a JavaScript function that processes a numeric string and correctly handles leading zeros when inserting dashes.
- Write a JavaScript function that validates the input and returns the original string if no consecutive even digits exist.
- Write a JavaScript function that implements dash insertion without converting the string into an array.
Improve this sample solution and post your code through Disqus.
Previous: Write a simple JavaScript program to join all elements of the following array into a string.
Next: Write a JavaScript program to sort the items of an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics