repl.it
@MMarkosPro21/

calsulator

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let str = `(2)+(5-2)-((3)/(5+2))`;
const exceptions = ['.'];

let symbols = [-1];
let splits = [];
let brackets = [];

[...str].forEach((c, i) => {
  let num = parseInt(c);

  // find symbols
  if (isNaN(num) && !exceptions.includes(c)) {
    symbols.push(i)
  }

})

symbols.forEach((i, x) => {
  splits.push(str.substring(i + 1, symbols[x+1]))
})

// remove empty strings from numbers
splits = splits.filter(e=>e!=="")

// splits.forEach((e,i)=>str = str.replace(e,i))

console.log(str)
console.log(splits);

symbols = [];

let opBracks = [];
let closeBracks = [];
for(var i = 0; i<str.length; i++){
  if(str[i]==="("){
    opBracks.push(i);
  }
}

opBracks = opBracks.sort(function(a, b){return a-b}).reverse();
console.log(`zzzz${opBracks.sort()}`)
opBracks.forEach((e,i)=>{
  for(j=e; j<str.length; j++){
    if(str[j] === ")" && !closeBracks.includes(j)){
      closeBracks.push(j)
      break;
    }
  }
})
console.log(closeBracks)

for(i = 0; i<opBracks.length; i++){
  console.log(str.substring(opBracks[i],closeBracks[i]+1))
}

// [...str].forEach((c,i)=>{
//   //find brackets
  
//   if(c==="("){
//     for(j=i; j<str.length; j++){
//       if(str[j]==")"){
//         brackets.push([i,j])
//         break;
//       }
//     }
//   }
// })


// ((((1-1)+1)*9)/10)