Passed
Push — master ( 6fd7ff...c29376 )
by Nguyen
47s queued 12s
created

topics/stack/postfix-to-infix/index.js   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 28
Function Count 1

Duplication

Duplicated Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
mnd 3
bc 3
fnc 1
dl 28
loc 28
rs 10
bpm 3
cpm 4
noi 1
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
const Stack = require('../index.js');
2
const operators = {
3
    "+": true,
4
    "-": true,
5
    "*": true,
6
    "/": true,
7
}
8
const PostfixToInfix = (formula) => {
9
    let result = ""
0 ignored issues
show
Unused Code introduced by
The variable result seems to be never used. Consider removing it.
Loading history...
10
    let stack = new Stack()
11
    for (let i = 0 ; i < formula.length ; i++) {
12
        let c = formula[i]
13
        if (!operators[c]) {
14
            stack.push(c)
15
            continue
16
        }
17
        let subFormula = stack.pop()
18
        subFormula = stack.pop() + c + subFormula
19
        if (i !== formula.length - 1) {
20
            stack.push('(' + subFormula + ')')
21
        } else {
22
            stack.push(subFormula)
23
24
        }
25
    }
26
    return stack.pop()
27
}
28
module.exports = PostfixToInfix
29