简体   繁体   English

如何成功突破javascript中的功能?

[英]How to successfully break out of function in javascript?

So I have a function that checks if the last character in a string is an arithmetic operator(+,-,*,/), and if it is it should immediately return from the function. 因此,我有一个检查字符串中最后一个字符是否为算术运算符(+,-,*,/)的函数,如果是,则应立即从函数中返回。 If not, the subtract sign should be appended to the string. 如果不是,则应在字符串后附加减号。 However, only the second case seems to be occurring and I can't figure out why. 但是,似乎只有第二种情况在发生,我不知道为什么。 If anyone could provide some insight I would be very appreciative. 如果有人能提供一些见识,我将不胜感激。

    $("#subtract").click(function () {
        var original=$("#display").text();
        var sliced=original.slice(0,original.length - 1);
        var signs=["+","-","*","/"];
        var charpos=sliced.charAt(sliced.length -1);

        if ((charpos === signs[0]) || (charpos === signs[1]) ||  (charpos  === signs[2]) || (charpos === signs[3])) {
            return;
        }
        else {
            var newdisplay=sliced + "-";
            $("#display").text(newdisplay);
        }

     });
var original=$("#display").text();
var sliced=original.slice(0,original.length - 1);

the second line gets rid of the last character in your string. 第二行摆脱了字符串中的最后一个字符。 so you're not even able to check what that was. 因此您甚至无法检查那是什么。 just get rid of it and change the first line to 摆脱它,将第一行更改为

var sliced=$("#display").text();

and everything should work fine 一切都应该正常

Instead of 代替

if ((charpos === signs[0]) || (charpos === signs[1]) ||  (charpos  === signs[2]) || (charpos === signs[3])) {
        return;
    }

try 尝试

if ($.inArray(charpos, signs) > -1) {
    return;
}

First of all, never enumerate items from an array in such a fashion - it's easy to miss items that way. 首先,永远不要以这种方式枚举数组中的项目-这样很容易错过项目。 Secondly, exact instance comparison (===) won't fire, unless you compared exact same strings. 其次,除非您比较完全相同的字符串,否则不会触发确切的实例比较(===)。

Besides, consider revising how you extract the last character. 此外,请考虑修改如何提取最后一个字符。

You're slicing the input which is removing the last character, then using charAt and getting the second to last character. 您要对输入进行切片,该输入将删除最后一个字符,然后使用charAt并获取倒数第二个字符。

var original=$("#display").text(); // "foobar"
var sliced=original.slice(0,original.length - 1); // "fooba"
var charpos=sliced.charAt(sliced.length -1); // "a" -- we lost the "r"

You only need to call charAt and work with the last character (or, if you prefer slice , it's able to accept a negative number and backtrack the string). 您只需要调用charAt并使用最后一个字符即可(或者,如果您喜欢slice ,它可以接受一个负数并回溯该字符串)。

Secondly, since signs is an array, you can use indexOf over checking each element. 其次,由于signs是一个数组,因此可以使用indexOf来检查每个元素。 eg 例如

var original = $('#display').text();
var signs = ["+","-","*","/"];
var lastChar = original.slice(-1); // OR original.charAt(original.length - 1);

// Check for the lastChar in the signs array
if (signs.indexOf(lastChar) != -1){ // -1 == not found
  return; // short exit
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM