Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jason chan
jason chan
31,009 Points

Why is my angular function concatenating instead of adding?

html

<!DOCTYPE html>
<html ng-app="myFirstApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Angular Training</title>
<link rel="shortcut icon" href="">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<style>body{padding-top:50px;}.starter-template{padding:40px 15px;text-align:center;}</style>

<!--[if IE]>
<script src="https://cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Angular Course</a>
</div>

<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--.nav-collapse -->
</div>
</nav>

<div class="container">
<div class="starter-template" ng-controller='myController'>

<div class="input-group" >
    <label for="firstName">Integer1 
        <input type="text" name="name"  ng-model="color1"> 
    </label>
</div>
<div class="input-group">
    <label for="lastName">Interger2
        <input type="text" name="name"  ng-model="color2"> 
    </label>
</div>


<h1>Hello, there {{color1}} * {{color2}} = {{printName()}}</h1>
<br>
<h2>This is plus {{add()}}</h2>
<h2>This is subtract {{subtract()}}</h2>
</div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js'></script>
<script src="app.js"></script>

</body>
</html>

app.js

var myApp = angular.module('myFirstApp', []);
myApp.controller('myController', function($scope) {

    $scope.color1 = 5;
    $scope.color2 = 6;

    $scope.printName = function() {

       return $scope.color1 * $scope.color2;
    }
    $scope.add = function() {
        return $scope.color1 + $scope.color2;
    }
    $scope.subtract = function () {
        return $scope.color1 - $scope.color2;
    }
} );

subtract works? How come when I type in numbers for addition function it concatinates instead of adds? Do i have to parsInt() ?

4 Answers

Marc Schultz
Marc Schultz
23,356 Points

Yes, you need parseInt() or parseFloat() because there are no strings in this expression.

How about this:

$scope.color1 + '' + $scope.color2;
jason chan
jason chan
31,009 Points

The answer was to wrap the addition function in parenthesis. Just like math. Please excuse my dear aunt sally. LOLs.

var myApp = angular.module('myFirstApp', []);
myApp.controller('myController', function($scope) {

    $scope.color1 = 5;
    $scope.color2 = 6;

    $scope.printName = function() {

       return $scope.color1 * $scope.color2;
    }
    $scope.add = function() {
        return ($scope.color1 + $scope.color2);
    }
    $scope.subtract = function () {
        return $scope.color1 - $scope.color2;
    }
} );
jason chan
jason chan
31,009 Points

i need help again it's doing it again in NG-CLICK oh man!!!!

<!DOCTYPE html>
<html ng-app="myFirstApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Angular Training</title>
<link rel="shortcut icon" href="">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<style>body{padding-top:50px;}.starter-template{padding:40px 15px;text-align:center;}</style>

<!--[if IE]>
<script src="https://cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Angular Course</a>
</div>

<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--.nav-collapse -->
</div>
</nav>

<div class="container">
<div class="starter-template" ng-controller='myController'>

<div class="input-group" >
    <label for="firstName">Integer1 
        <input type="text" name="name"  ng-model="color1"> 
    </label>
</div>
<div class="input-group">
    <label for="lastName">Interger2
        <input type="text" name="name"  ng-model="color2"> 
    </label>
</div>


<h1>Hello, there {{color1}} * {{color2}} = {{printName()}}</h1>
<br>
<h2>This is plus {{add()}}</h2>
<h2>This is subtract {{subtract()}}</h2>
<h2>This is divide: {{divide()}}</h2> <br>
<button ng-click="sayHello()">Greet</button>
{{hi}}
<br>
<button ng-click="clickPlus()">Add Please</button>
<button ng-click="why = why + 1">++</button>
{{why}}
</div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js'></script>
<script src="app.js"></script>

</body>
</html>
var myApp = angular.module('myFirstApp', []);
myApp.controller('myController', function($scope) {

    $scope.color1 = 5;
    $scope.color2 = 6;
    $scope.why = 0;

    $scope.printName = function() {

       return $scope.color1 * $scope.color2;
    }
    $scope.add = function() {
        return ($scope.color1 + $scope.color2);
    }
    $scope.subtract = function () {
        return $scope.color1 - $scope.color2;
    }
    $scope.divide = function () {
        return ($scope.color1 / $scope.color2).toFixed(2);
    }
    $scope.sayHello = function() {
        $scope.hi = 'Hello ' + $scope.color1 + '!';
    }
    $scope.clickPlus = function() {
        $scope.why = $scope.color1;
    }
} );

Whenever I type in a new number in the form it concats instead of adds. NG CLICK. ughhh.

jason chan
jason chan
31,009 Points
var myApp = angular.module('myFirstApp', []);
myApp.controller('myController', function($scope) {

    $scope.color1 = 5;
    $scope.color2 = 6;
    $scope.why = 0;

    $scope.printName = function() {

       return $scope.color1 * $scope.color2;
    }
    $scope.add = function() {
        return ($scope.color1 + $scope.color2);
    }
    $scope.subtract = function () {
        return $scope.color1 - $scope.color2;
    }
    $scope.divide = function () {
        return ($scope.color1 / $scope.color2).toFixed(2);
    }
    $scope.sayHello = function() {
        $scope.hi = 'Hello ' + $scope.color1 + '!';
    }
    $scope.clickPlus = function() {
        // this +(string) converts string into number
        $scope.why = +($scope.color1);
    }
} );
//shorthand for string conversion to int
+('5') // converts to 5
WOOOHOOO! fixed me bug