Compare two last character in a string

I am programming a calculator in AngularJS. I am stuck on a validating user input. I do not want the user to be able to enter two 2 operators ('+','/','*') next to each other. Thus every time, I try to compare the last character and the second to last character of the string. But I always find I have two operator characters.

var app = angular.module("myApp", []);

app.controller("myCtrl", function ($scope) {

  $scope.expression = "";

  var liste = ['+', '/', '*'];

  $scope.add = function (ope) {

    $scope.expression += String(ope);

    var der = $scope.expression[$scope.expression.length - 1];
    var avantDer = $scope.expression[$scope.expression.length - 2];

    if ($scope.expression.length > 3 && liste.includes(der) && liste.includes(avantDer)) {
      alert("error");
    } else {
      $scope.expression += String(ope);
    }
  };
});

Answers:

Answer

You are very close. The problem is that you are adding the operator to the expression before you have checked if it is valid or not. It is better to check the last character of the existing expression and the new character as a separate variable.

You also want to check if the length of expression is greater than 0 rather than 3 as otherwise, the user could enter two '+' characters straight away when the length is less than 3.

var app = angular.module("myApp", []);

app.controller("myCtrl", function ($scope) {

  $scope.expression = "";

  var liste = ['+', '/', '*'];

  $scope.add = function (ope) {

    // don't add to expression, just store into der
    var der = String(ope);
    var avantDer = $scope.expression[$scope.expression.length - 1];

    if ($scope.expression.length > 0 && liste.includes(der) && liste.includes(avantDer)) {
        alert("error");
    } else {
        $scope.expression += der;
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <div>
    <button ng-click="add('+')">+</button>
    <button ng-click="add('*')">*</button>
    <button ng-click="add('/')">/</button>
  </div>
  <div>
    <button ng-click="add('1')">1</button>
    <button ng-click="add('2')">2</button>
    <button ng-click="add('3')">3</button>
  </div>
  {{expression}}
</div>

Answer

There were two things wrong.

  1. $scope.expression.length > 3 should have been $scope.expression.length > 2
  2. You were calling $scope.expression += String(ope); twice

I made a minor change below so I could run it in the code snippet window.

I also added subtraction to liste.

var $scope = {
  expression: ""
};


var liste = ['+', '/', '*', '-'];

debugger
$scope.add = function (ope) {
  var temp = $scope.expression + String(ope);
  console.log(temp);
  var len = temp.length - 1;

  if (len > 1) {
    var der = temp[len];
    var avantDer = temp[len - 1];

    if (liste.includes(der) && liste.includes(avantDer)) {
      console.log("error");
    } else {
      $scope.expression = temp;
    }
  }
  else {
    $scope.expression = temp;
  }
};

$scope.add('3');
$scope.add('+');
$scope.add('-');

When I call $scope.add('-'); it displays the error like you expect.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.