Write a JavaScript program which accept a number as input and insert dashes (-) between each two even numbers. For example if you accept 025468 the output should be 0-254-6-8

 <!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="result"></div>
</body>
<script>
    var num = window.prompt();
    var str = num.toString();
    var f = [str[0]]
    alert(typeof(f));
    for (let index = 0index < str.lengthindex++) {
        if((str[index-1]%2===0) || (str[index]%2===0)){
            f.push('-', str[index]);
        }else{
            f.push(str[index]);
        }

    }
    alert(f.join(' '))
</script>
</html>

Comments

Popular posts from this blog

Write a JavaScript program to get the current date.

Store and display the values of text boxes of a form

Write a JavaScript program to count and display the items of a dropdown list, in an alert window