0

i am creating calculator when i click plus(+) operator i put the value in global variable which i get from input field and i do blank input field, but i have to take value from input which i enter second time and i have to keep another variable. that is i am trying to do but i don't understand how to do this.

JS Fiddle URL: http://jsfiddle.net/ravianexpert/9q6Xu/1

Please help me

var operation="";
var restValue="";
var lastValue="";

function operator(op){
    if(op == '+'){
        alert("Plus");
        restValue = Number(document.getElementById("display").value);
        document.getElementById("display").value="";
        if(restValue == ""){
            alert("Zero");
        }else{
            lastValue = Number(document.getElementById("display").value);
        }
        //document.getElementById("display").value=restValue + lastValue;
    }
    console.log("restValue : " + restValue);
    console.log("lastValue : " + lastValue);
    console.log("operation : " + operation);
    //lastValue = restValue.charAt(restValue.length - 1);
    //console.log(lastValue)
}

<div class="container">
    <div id="lastCalculation"></div>
    <input type="text" step="any" size="16" value="0" id="display" readonly />
    <div class="keypad">
        <span class="clear" onclick="reset()">C</span>
        <span class="del" onclick="deleteValue()">Del</span>
        <div class="clr"></div>
        <span onclick="setNum(7)">7</span>
        <span onclick="setNum(8)">8</span>
        <span onclick="setNum(9)">9</span>
        <span onclick="operator('/')">/</span>
        <span onclick="setNum(4)">4</span>
        <span onclick="setNum(5)">5</span>
        <span onclick="setNum(6)">6</span>
        <span onclick="operator('*')">*</span>
        <span onclick="setNum(1)">1</span>
        <span onclick="setNum(2)">2</span>
        <span onclick="setNum(3)">3</span>
        <span onclick="operator('-')">-</span>
        <span onclick="setNum(0)">0</span>
        <span onclick="setNum('.')">.</span>
        <span class="isEqualTo" onclick="calculate()">=</span>
        <span onclick="operator('+')">+</span>
        <div class="clr"></div>
    </div><!-- keypad -->
</div><!-- container -->
Asons
  • 84,923
  • 12
  • 110
  • 165
Ravi
  • 95
  • 9
  • Uncaught ReferenceError: setNum is not defined – manta Feb 27 '14 at 09:31
  • Ravi prepare a demo fiddle please – Deepak Ingole Feb 27 '14 at 09:31
  • ok pilot i am trying to do in fiddle once i complete let you know. – Ravi Feb 27 '14 at 09:35
  • @ Pilot please find the link of js fiddle http://jsfiddle.net/ravianexpert/9q6Xu/ – Ravi Feb 27 '14 at 09:40
  • and i don't know why there is displaying the error "set num is not defined" – Ravi Feb 27 '14 at 09:42
  • @Ravi: In the fiddle you need to set it to *"wrap in head"* (menu on left side, second drop-down). It indicates where your script should be placed in the resulting document shown in the lower right window. – user13500 Feb 27 '14 at 09:57
  • yes its working now. i am sharing new url of js fiddle please me. @user13500 : http://jsfiddle.net/ravianexpert/9q6Xu/1/ – Ravi Feb 27 '14 at 10:07
  • @ pilot please find the new fiddle URL : http://jsfiddle.net/ravianexpert/9q6Xu/1/ – Ravi Feb 27 '14 at 10:07
  • Updated my answer...with a demo as well – Asons Feb 27 '14 at 10:26
  • @Ravi: Is this some sort of school project? Can you use prototype etc. with `this` ? – user13500 Feb 27 '14 at 10:30
  • @user13500 sorry i have to use only javascript even i don't have to use "eval". :) – Ravi Feb 27 '14 at 11:01
  • @Ravi: `eval` is (in most cases) evil and something you should stay far away from. `prototype` as well as `eval` *is* Javascript, but if you are learning trough some course then it usually means that you only can use "*what you have learned so far*". That is why I asked if it was a school project. – user13500 Feb 27 '14 at 11:06
  • @user13500 hmmm it is not school project its my own project which is given by my one of the childhood friend he kept some conditions for it like i don't have to use "eval". actually i all ready done by "eval". – Ravi Feb 27 '14 at 11:11

1 Answers1

1

Updated 2016-05-29

Ran into a great answer by @yckart, that doesn't use the evil eval() but the Function constructor, so here is a demo showing how you can do to make the calculation work properly.

document.querySelector('button').addEventListener('click', function (e) {

  var op = document.querySelector('input').value;        
  alert('Using new Function: ' + calculate(op) + '\nUsing eval(): ' + evil_eval(op));   // if default is used => 40.4
  
})

function calculate(op){
  return new Function('return ' + op)();
}

// added for test purpose, showing they return the same value
function evil_eval(op) {
  return eval(op);
}
<input type="text" value="12/5*9+9.4*2"> <button>Calc</button>

Src: Calculate string value in javascript, not using eval

Community
  • 1
  • 1
Asons
  • 84,923
  • 12
  • 110
  • 165
  • Well, dealing with issues like "empty" value etc., Ravi will fix – Asons Feb 27 '14 at 10:24
  • Thanks @PellePenna you did for me but i don't have to use "eval" for this, so i am doing, if i get any resolution for it i will post here. Thank you very much. :) – Ravi Feb 27 '14 at 11:04