Have the function DashInsert(str) insert a dash ('-') after each odd number in str

Hello, I’m a JS noob looking for some help.

let str = 1002505;

let dash = str.toString().split('');

const threshold = str.toString().replace(/[^02468]/g, dash.every(i => i.concat(i,'-')));


// expected result: 1-0025-05-

// Current output: true002true0true

Welcome, zobisco.

You are making it too complicated. The replace method can do what you are looking for, by passing a function as the second argument: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

This is the spoiler if you get stuck:

const threshold = str.toString().replace(/[^02468]/g,i => i +'-');

Hope that helps.

1 Like