-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
53 lines (53 loc) · 1.67 KB
/
Javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const number = readline();
const D = parseInt(readline());
let possibleAnswer = []; //Answer respecting the rules
if (isDivByD(number)) {
//0 Deleted Digit
console.log(number);
} else {
for (let nbDeletedDigit = 1; nbDeletedDigit <= 2; nbDeletedDigit++) {
for (let i = 0; i < number.toString().length; i++) {
let a = Array.from(String(number), Number); //Convert number to array
a.splice(i, 1); //Delete 1 digit
if (nbDeletedDigit == 2) {
//If we delete 2 digits
for (let j = 0; j < arrayToNumber(a).toString().length; j++) {
let b = [...a];
b.splice(j, 1); //Delete another digit
if (isDivByD(arrayToNumber(b)) && !arrayToNumber(b).match(/^0+.+/)) {
//Check if number respecting the rules (Regex is here because if we delete a 0 its probably more then 2 digits)
possibleAnswer.push(arrayToNumber(b));
}
}
} else {
//If we delete 1 digit
if (isDivByD(arrayToNumber(a)) && !arrayToNumber(a).match(/^0+.+/)) {
//Check if number respecting the rules (Regex is here because if we delete a 0 its probably more then 2 digits)
possibleAnswer.push(arrayToNumber(a));
}
}
}
}
if (possibleAnswer.length == 0) {
//none number respecting rule then log 0
console.log(0);
} else {
possibleAnswer.sort(compare);
console.log(parseInt(possibleAnswer[0]));
}
}
function isDivByD(n) {
return n % D == 0;
}
function arrayToNumber(arr) {
return arr.toString().replaceAll(",", "");
}
function compare(a, b) {
if (parseInt(a) > parseInt(b)) {
return -1;
}
if (parseInt(a) < parseInt(b)) {
return 1;
}
return 0;
}