MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / maximum69Number

Function maximum69Number

javascript/1323-maximum-69-number.js:5–23  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

3 * @return {number}
4 */
5var 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++) {
11 // loop through the every element of array numArr
12
13 if (numArr[i] == 6) {
14 // if current element of numArr is 6
15 let arr = [...numArr]; // copy numArr into arr
16 arr.splice(i, 1, 9); // make current element arr to 9
17 maxArr.push(arr.join('')); // convert arr to string and push into maxArr and break the loop
18 break;
19 }
20 }
21
22 return Math.max(...maxArr); // return max value from maxArr
23};

Callers

nothing calls this directly

Calls 2

toStringMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected