Noob is looking for help. I have a function that takes a string of braces and check if it follows some rules
"(){}[]" => True
"([{}])" => True
"(}" => False
"[(])" => False
"[({})](]" => False
Solution is simple: keep adding to result array a left braces till you face a right braces(I add it to an array and then slice them) but when I want to slice or pop 2 last elements of the result array it doesnt work
function validBraces(braces){
return [...braces].map((currentBrace,index) => {
let result = []
if(currentBrace == '{' | currentBrace == '(' | currentBrace == '['){
result += (currentBrace) //If I use push I get as a result [ [], [], etc.]
}
if(currentBrace == ')' | currentBrace == ']' | currentBrace == '}'){
result.slice(0, -2)
}
return result//.length == 0 ? true : false
})
}
How I can slice an array of elements inside this function?
array.map()returns an array of all the results of the callback function. So if your function returns an array, the final result will be a 2-dimensional array..map()if you want the function to return a boolean?currentBraceactually matches the one at the top of the stack,result.slice(0, -2)is not mutating the array, initialisingresultinside the loop, returning inside of the loop instead of afterwards, the whole usage ofmapinstead of a loop, the nameresultfor the stack, the commented out.length == 0, the unnecessary conditional operator…