Created
February 20, 2021 13:50
-
-
Save Frdrcpeter007/b36ec182986c3e56f29cad15f736c28c to your computer and use it in GitHub Desktop.
Cette fonction insère une - entre deux nombre impair dans une chaine, on peut l'utiliser pour faire des slugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Cette insère une - entre deux nombre impair dans une chaine | |
* @param {String} str | |
* @return {String} | |
*/ | |
function dashInsert(str) { | |
let newString = str[0] || ''; | |
for (let i = 1, lastNumberIsOdd = str[0] % 2 === 1; i < str.length; i++) { | |
const numberIsOdd = str[i] % 2 === 1; | |
newString += numberIsOdd && lastNumberIsOdd ? '-' + str[i] : str[i]; | |
lastNumberIsOdd = numberIsOdd; | |
} | |
return newString; | |
} | |
module.exports = dashInsert; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment