比较字符串中的两个最后一个字符 [英] Compare two last character in a string

查看:124
本文介绍了比较字符串中的两个最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS中编写计算器。我被困在验证用户输入上。我不希望用户能够彼此相邻地输入两个2个运算符('+','/','*')。
因此,每次,我都会尝试比较字符串的最后一个字符和倒数第二个字符。但我总是发现我有两个操作符。

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

app.controller(myCtrl,函数($ 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);
}
};
});


解决方案

你非常接近。问题是您在检查表达式是否有效之前将运算符添加到表达式中。最好检查现有表达式的最后一个字符和新字符作为单独的变量。



您还要检查表达式大于0而不是3,否则,当长度小于3时,用户可以立即输入两个+字符。



< pre class =snippet-code-js lang-js prettyprint-override> var app = angular.module(myApp,[]); app.controller(myCtrl,function($ scope) ){$ scope.expression =; var liste = ['+','/','*']; $ scope.add = function(ope){//不要添加到表达式,只是存储到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);} els e {$ scope.expression + = der; }};});

 < script src = https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js\"></script><div ng-app =myAppng-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> {{表达}}< / div>  


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);
    }
  };
});

解决方案

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>

这篇关于比较字符串中的两个最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆