Created
April 29, 2020 18:16
-
-
Save gtindo/41f532aff60cd9a0e609c0b65b652eb8 to your computer and use it in GitHub Desktop.
validate operation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isCorrect(expr){ | |
const n = expr.length; | |
const numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]; | |
const beforeOperator = [...numbers, ")"] | |
const afterOperator = [...numbers, "("] | |
const operators = ["+", "-", "/", "*", "."]; | |
const authorisedsChars = [...numbers, ...operators, "(", ")"]; | |
let leftParenthesis = []; | |
for(let i = 0; i < n; i++){ | |
let c = expr[i]; | |
// Check if character is authorized | |
if(!authorisedsChars.includes(c)){ | |
console.log(`Character "${c}" is invalid`); | |
return false; | |
} | |
// Check if operators are correct | |
if(operators.includes(c)){ | |
if(i === n-1) { | |
console.log(`Character ${c} can't be the last character.`) | |
return false; | |
}else if(i === 0) { | |
console.log(`Character ${C} can't be the first character.`) | |
return false; | |
}else if( | |
!beforeOperator.includes(expr[i-1]) && | |
!afterOperator.includes(expr[i+1]) | |
){ | |
console.log(`Characters surrounding ${c} must be numbers.`) | |
return false; | |
} | |
} | |
// Check successive parenthesis | |
if(c === "(" && i < n-1 && expr[i+1] === ")"){ | |
console.log(`Character "${c}" can't be follow by ")"`) | |
} | |
// Check parenthesis closing order | |
if(c === "(") leftParenthesis.push(c); | |
if(c === ")") { | |
if(leftParenthesis.lenght === 0) return false; | |
else leftParenthesis.pop() | |
} | |
} | |
if(leftParenthesis.length > 0) return false; | |
return true | |
} | |
console.log(isCorrect("100*2+6")) | |
console.log(isCorrect("100*2*12+(1+2)")) | |
console.log(isCorrect("100*(2+12)/14")) | |
console.log(isCorrect("3.78+4.78*(1.55)")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment