I'm a coding beginner & basically trying to modify the javascript code below (which I found online - Permutations in JavaScript?) to do the following:
- return only the permuted numbers in a mixed string & in descending order (e.g. If the provided input is: “D 3fs m4q”, then the solution should return "43, 34".
- return an error exception message if the input provided does not contain any numbers (e.g. If the provided input is: “GFA”, return null)
P.S I already figured out how to implement the descending function, however, when I try to use the .replace method to extract just the numbers in the string, it doesn't work for me. What am I doing wrong?
var permArr = [], usedChars = [];
function permute(input) {
permArr.sort(function(a, b) {return b - a})
permArr.replace(/\D/g);
var i, ch, chars = input.split("");
for (i = 0; i < chars.length; i++) {
ch = chars.splice(i, 1);
usedChars.push(ch);
if (chars.length == 0)
permArr[permArr.length] = usedChars.join("");
permute(chars.join(""));
chars.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
console.log(solution("458")); //console.log(result);
Can the solution please be in javascript, as I am trying to get a grasp of the language? Any assistance would be greatly appreciated. Thanks for your help in advance.