Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions assets/js/numbers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
function compute(expression) {
// TODO - write method definition here
}
var flag = -1; //keep track of */ operations
var result = 0;
var op = new Array();
var val = new Array();
var num = "";
for(var i = 0; i <= expression.length; i++){
if(i == expression.length){
if(flag == 1) {
var o = op.pop();
if(o == "*"){
val.push(parseInt(num) * val.pop());
}
else if(o == "/"){
val.push( val.pop() / parseInt(num));
}
flag = -1;
}
}
if(expression[i] == "+" || expression[i] == "-"){
if(flag == 1) {
var o = op.pop();
if(o == "*"){
val.push(parseInt(num) * val.pop());
}
else if(o == "/"){
val.push(val.pop() / parseInt(num));
}
flag = -1;
}
else {
val.push(parseInt(num));
}
op.push(expression[i]);
num = "";
}
else if(expression[i] == "*" || expression[i] == "/"){
if (flag == 1){
var o = op.pop();
if(o == "*"){
val.push(parseInt(num) * val.pop());
}
if(o == "/"){
val.push(val.pop() / parseInt(num));
}
}
else {flag = 1;
val.push(parseInt(num));
}
op.push(expression[i]);
num = "";
}
else {
num = num + expression[i];
}
}
val.push(parseInt(num));

num = val[0];
for(var index = 0; index < op.length; index ++){
if(op[index] == "+")
num = num + val[index+1];
else num = num - val[index+1];
}
return num;
}

3 changes: 0 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
<!-- ---------------------------------------------------------------------------------- -->
<!-- ---------------------------------------------------------------------------------- -->
<body> <!-- body begins here -->

<!-- ====================================================== -->
<!-- ====================================================== -->
<footer>
<!-- footer of page begins here -->
<script type="text/javascript" src="./assets/js/footer-functions.js"></script>
Expand Down