Skip to content

Commit 3334963

Browse files
Merge pull request neetcode-gh#2823 from chetannada/ltc-1323-js
Create 1323-maximum-69-number.js
2 parents 5d398d0 + d3482da commit 3334963

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

javascript/1323-maximum-69-number.js

Lines changed: 21 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy