// Don't need to handle these special control characters
// Slash (/) \/
// Backspace \b
// Form feed \f
// New line \n
// Carriage return\r
// Horizontal tab \t
// Input:
// {"key1":"val1","key2":["item1",{"item2key":1.2}]}
// Output:
// {
// "key1": "val1",
// "key2": [
// "item1",
// {
// "item2key": 1.2
// }
// ]
// }
const input = "{\"key1\":\"val1\",\"key2\":[\"item1\",{\"item2key\":1.2}]}";
const input2 = "{\"key,1\":\"val1\",\"key2\":[\"item1\",{\"item2key\":1.2}]}";
const solution = (somestr) => {
let indentCount = 0;
let finalStr = "";
let isOpen = false;
for(let i = 0; i < somestr.length; i++) {
const char = somestr[i];
if(char === "{") {
finalStr += char;
finalStr += "\n";
indentCount++;
finalStr += "\t".repeat(indentCount);
}
else if(char === "}") {
finalStr += "\n";
indentCount--;
finalStr += "\t".repeat(indentCount);
finalStr += char;
}
else if (char === "[") {
finalStr += char;
finalStr += "\n";
indentCount++;
finalStr += "\t".repeat(indentCount);
}
else if (char === "]") {
finalStr += "\n";
indentCount--;
finalStr += "\t".repeat(indentCount);
finalStr += "]";
}
else if (char === ",") {
finalStr += char;
finalStr += "\n"
finalStr += "\t".repeat(indentCount);
}
else {
if(char === '"') {
isOpen = true
}
finalStr += char;
i++;
while(isOpen) {
const newChar = somestr[i];
finalStr += newChar;
if(newChar === '"') {
isOpen = false;
}
i++;
}
i--;
}
}
console.log(finalStr)
}
solution(input2)I completed the question with extra time left over but still was denied - not sure why. but here is the question they asked. you get a string and you need to format it and console log the formatted string.