Skip to content

Commit d3482da

Browse files
committed
Create 1323-maximum-69-number.js
1 parent b0f3f52 commit d3482da

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

javascript/1323-maximum-69-number.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
var maximum69Number = function (num) {
6+
let maxArr = [num]; // initialize array maxArr with num as value
7+
8+
let numArr = num.toString().split(''); // initialize numArr as array of num
9+
10+
for (let i = 0; i < numArr.length; i++) { // loop through the every element of array numArr
11+
12+
if (numArr[i] == 6) { // if current element of numArr is 6
13+
let arr = [...numArr] // copy numArr into arr
14+
arr.splice(i, 1, 9); // make current element arr to 9
15+
maxArr.push(arr.join('')); // convert arr to string and push into maxArr and break the loop
16+
break;
17+
}
18+
}
19+
20+
return Math.max(...maxArr); // return max value from maxArr
21+
};

0 commit comments

Comments
 (0)