0

Here is my code where I am implementing a simple calculator in TypeScript .The problem here is that all the operations are working fine except addition where instead of addition, concatenation of numbers is performed.

HTML CODE :

<input type="text" [(ngModel)]="n1" />
 <input type="text" [(ngModel)]="n2" />

<h1>Number 1 : {{n1}}</h1>
<h1>Number 2 : {{n2}}</h1>

<select (change)="handle(selectField.value);" #selectField>
<option value="addition">Addition</option>
<option value="subtraction">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="divide">Division</option>

</select>


<h1>Result = {{result}}</h1>  

My Angular Code is as follows:

import {
  Component,
  OnInit
} from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  n1: number = 0;
  n2: number = 0;
  result: number = 0;

  handle(value: string) {

    if (value == "addition") {
      this.result = this.n1 + this.n2;
    } else if (value == "subtraction") {
      this.result = this.n1 - this.n2;
    } else if (value == "multiply") {
      this.result = this.n1 * this.n2;
    } else if (value == "divide") {
      this.result = this.n1 / this.n2;
    }

  }

  constructor() {}
  ngOnInit() {

  }

}

I have declared both n1 and n2 as numbers and also the result as number. So why is it that the numbers are being treated as strings instead of integers?

Note: I have gone through some other Stack Overflow posts on similar issue but couldn't find the possible solution.

Please help!

6
  • 4
    They're being treated as strings because they are strings. I assume you're binding some elements in the UI to n1 and n2? Commented Jun 21, 2017 at 16:03
  • 3
    Note that TypeScript only performs compile-time type checking, not run-time. Commented Jun 21, 2017 at 16:04
  • 1
    You can resolve it. this.result=(+this.n1)+(+this.n2); Commented Jun 21, 2017 at 16:07
  • @Amy I have updated my code with the html code Commented Jun 21, 2017 at 16:07
  • 1
    The + sign in front coerces the value into a number. See the many ways you can achieve this: stackoverflow.com/questions/1133770/… Commented Jun 21, 2017 at 16:12

2 Answers 2

2

You can resolve it. this.result=(+this.n1)+(+this.n2);

Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
1

Any value from an HTML input is automatically string. To resolve this, you must cast/convert your input value to a number. The (+this.n1) hack does it, but to really do it right you should cast or convert your values.

this.result = parseInt(this.n1) + parseInt(this.n2);

This is just one way. There are several others.

1 Comment

I should also note that this method is prone to failures if the value from the input evaluates to NAN.

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.