E
E
evgemiil2016-09-14 10:39:59
JavaScript
evgemiil, 2016-09-14 10:39:59

How to make the counter not go less than zero (negative numbers)?

Below is the code, but the decrement goes into a minus, how to make it not go into a minus?
here is how i fixed it

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
<script> angular.module("CounterApp", []) 
.controller("CounterController1", function($scope) { 
$scope.a = 0;
 $scope.b = 0;
 $scope.c = 0;
$scope.decrement = function() { 
if ($scope.a > 0) $scope.a--;
 if ($scope.b > 0) $scope.b--;
 if ($scope.c > 0) $scope.c--;
} }) 
</script> 
</head>
 <body> 
 <div ng-app="CounterApp"> 
 <div ng-controller="CounterController1"> <button ng-model="a" type="number" ng-click="decrement()">Decrement</button> {{a}} <button ng-model="a" type="number" ng-click="a = a + 1">Increment</button> </div>
  <div ng-controller="CounterController1"> <button ng-model="b" type="number" ng-click="decrement()">Decrement</button> {{b}} <button ng-model="b" type="number" ng-click="b = b + 1">Increment</button> </div>
   <div ng-controller="CounterController1"> <button ng-model="c" type="number" ng-click="decrement()">Decrement</button> {{c}} <button ng-model="c" type="number" ng-click="c = c + 1">Increment</button> 
   </div> 
   </div> 
   <h1>{{ a + b + c }}</h1> 
 </body> 
 </html>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2016-09-14
@evgemiil

The code is not visible, but something like
I suppose so
Well, or
As you like
UPD: the code has become visible

$scope.decrement = function() {
    if ($scope.counter > 0) $scope.counter--;
};

A
Alexey Ukolov, 2016-09-14
@alexey-m-ukolov

I don’t know Angular, but common sense tells me that like this:

$scope.decrement = function() {
    if ($scope.counter === 0) {
        return;
    }

    $scope.counter--;
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question