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 = 0; index < str.length; index++) {
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
Post a Comment