Title: #439 - Ternary Expression Parser - LeetCode JavaScript Solutions
Open Graph Title: #439 - Ternary Expression Parser - LeetCode JavaScript Solutions
X Title: #439 - Ternary Expression Parser - LeetCode JavaScript Solutions
Description: Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return the result of it. You can...
Open Graph Description: Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return the result of it. You can...
X Description: Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return the result of it. You can...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/ternary-expression-parser
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/ternary-expression-parser","name":"#439 - Ternary Expression Parser - LeetCode JavaScript Solutions","description":"Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return the result of it. You can...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} expression\n * @return {string}\n */\nvar parseTernary = function(expression) {\n const stack = [];\n\n for (let i = expression.length - 1; i >= 0; i--) {\n const char = expression[i];\n if (char !== ':' && char !== '?') {\n stack.push(char);\n } else if (char === '?') {\n const condition = expression[i - 1];\n const trueValue = stack.pop();\n const falseValue = stack.pop();\n stack.push(condition === 'T' ? trueValue : falseValue);\n i--;\n }\n }\n\n return stack[0];\n};","keywords":"LeetCode 439, #439 - Ternary Expression Parser, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/ternary-expression-parser |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/ternary-expression-parser/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0439-ternary-expression-parser.js |
| String | https://leetcodejavascript.com/tags/string |
| Stack | https://leetcodejavascript.com/tags/stack |
| Recursion | https://leetcodejavascript.com/tags/recursion |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow