I've written a solution to a Codewars challenge:
You have to create a function that takes a positive integer number and returns the next bigger number formed by the same digits:
12 ==> 21; 513 ==> 531; 2017 ==> 2071
I am getting the following error on Codewars: "Execution Timed Out (12000 ms)". I am a rookie, and would appreciate if anyone could give me some suggestions on optimizing my code, as this is something I know very little about.
I've made some minor tweaks but nothing that really reconstructs the solution from the ground up.
//yourtakes codea herenum and finds the next biggest num composed of the same digits.
function nextBigger(num) {
let newNum = 0;
let otherNum = 0;
let indicator = 0;
while (num > newNum) {
if (indicator === 0) {
otherNum = num;
indicator++;
continue;
}
if (
String(num)
.split("")
.sort()
.join("") ===
String(otherNum)
.split("")
.sort()
.join("")
) {
if (otherNum > num) {
newNum = otherNum;
}
}
otherNum++;
}
return newNum;
}