Reason why your code didn't work
You thought that num.replace(...) would actually change num. In JavaScript, primitive data-types (of which numbers are one of) cannot be changed. What num.replace(...) actually does is return a completely new string that is identical to num - except, of course, with the replacement.
function showLowestToHighest(num) {
let numAsString = num.toString();
let rawNumArray = [];
for (const character of numAsString) {
rawNumArray.push(Number.parseInt(character));
}
let sortedNumArray = [];
for (let index = 0; index < numAsString.length; index++) {
let smallestNum = Math.min(...rawNumArray);
sortedNumArray.push(smallestNum);
let smallestNumIndex = rawNumArray.indexOf(smallestNum);
rawNumArray.splice(smallestNumIndex, 1);
}
console.log(sortedNumArray.join(''));
}
showLowestToHighest(894560);
How this function works
We establish rawNumArray - an array of the digits we're looking at. We then remove the smallest digit from rawNumArray and place it at the end of sortedNumArray, doing this until rawNumArray is empty. Finally, we log out the contents of sortedNumArray - as a string.
Weakness: if the number if larger then Number.MAX_SAFE_INTEGER it won't work.
num.lengthreturnsundefinednumlike astringbut it is anumber. Useconst num = "1234"sort? Is just an excercise, or does it have a serious use case?