0

Why this isn't working? I also did this by assigning the result back to the input field but that didn't work and still this is not showing an alert which should show the result..

    <script type="text/javascript">
    function calculate() {
       var calculateit=document.getElementById('disp');
       var pluscharacter=/+/;
       var matchplus=calculateit.search(pluscharacter);
       var inputlength=calculateit.length;
           if(matchplus!=-1 && matchplus!=0 matchplus!=inputlength) {
              answer=calculateit[0]+calculateit[1];
              alert("Your answer is: "+answer+" Is it?");
    }
    }
    </script>
6
  • What is the alert display? Or what is the error? Commented Dec 5, 2015 at 18:15
  • For better understanding, Please provide a working demo (code snippet,jsfiddle ...) Commented Dec 5, 2015 at 18:17
  • Syntax error at matchplus!=0 matchplus!=inputlength, learn how to use the console Commented Dec 5, 2015 at 18:17
  • actually i m new to javascripting and all that, i m assigned to make a simple calculator which should calculate some basic arithmetic and logical operation..here what i m doing is (getting the input entered by user and then after searching from it that is there any '+' character if there then split that input on the basis of this character and then two parts of input 1st one before plus character and 2nd one after the plus character, and then simply calculating the summation and displaying it back..but it isn't going in a good way.. Commented Dec 5, 2015 at 18:22
  • #Adeneo! here i m using (matchplus!=0 matchplus!=inputlength) to avoid inputs like this... (+32) or (43+) Commented Dec 5, 2015 at 18:26

3 Answers 3

2

Your if statement isn't a valid condition. Try:

if(matchplus!=-1 && matchplus!=0 && matchplus!=inputlength)
Sign up to request clarification or add additional context in comments.

Comments

1
var calculateit = document.getElementById('disp');
var matchplus = calculateit.search(pluscharacter);

calculateit is a Node doesn't have any search() method.

Comments

0

You're doing stuff you don't need to do - just split the string. You also need to get the innerHTML - THAT's the string. Or you can get the "value" from an input field instead. I'd trim it to get rid of the white space around the equation, though the parseInt would take care of that as well. I'd also push the answer into another div. Here's a jsfiddle:

https://jsfiddle.net/mckinleymedia/9ezky35v/2/

With this HTML:

<h3>Equation</h3>
<div id="disp">12+12</div>
<h3>Answer</h3>
<div id="answer"></div>

You can use this script:

function calculate() {
  var calculateit = document.getElementById('disp').innerHTML.trim(),
      numbers = calculateit.split('+'),
      answerDiv = document.getElementById('answer');
  if ( numbers.length > 1 ) {
    answer = parseInt(numbers[0]) + parseInt(numbers[1]);
    answerDiv.innerHTML = "Your answer is: <b>" + answer + "</b>, Right?";
  } else {
    answerDiv.innerHTML = "I can't calculate that equation.";
  }
}
calculate();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.