-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
24 lines (17 loc) · 668 Bytes
/
test.js
File metadata and controls
24 lines (17 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example Java code snippet with inconsistent indentation
const javaCode = ""
// Function to format Java code with proper indentation
function formatJavaCode(javaCode) {
// Split the code into lines
let lines = javaCode.split("\n");
lines = lines.map(line => line.trim());
const firstLineSpaces = lines[0].match(/^\s*/)[0].length;
lines = lines.map(line => {
const leadingSpaces = line.match(/^\s*/)[0].length;
const spacesToAdd = leadingSpaces - firstLineSpaces;
return " ".repeat(4) + line.trim().slice(spacesToAdd);
});
return lines.join("\n");
}
const formattedJavaCode = formatJavaCode(javaCode);
console.log(formattedJavaCode);