Is_sub-array-不返回false [英] Is_sub-array- not returning false

查看:72
本文介绍了Is_sub-array-不返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在尝试运行一个名为IsSubArray的程序。有2个输入文本框。一个称为大数组,另一个称为子数组。任务是检查子数组中的值是否连续匹配大数组中的数字。例如:

数组1:1,3,6,5,4

数组2:1,3,6 - 是因为1,3与数组1匹配。



数组2:1,6 - 假,因为在1之后阵列1中没有6。它应该连续匹配。



数组2:6,5,4 - 是真的。



数组2:5,4,7 - 错误因为没有数字7之后数组1中的4。

但是我对这最后的陈述是正确的。我无法弄明白为什么。



提前致谢:)





Hi. I'm trying to run a program called IsSubArray. There are 2 input textboxes. One is called the big array and the other is called the sub array. The task is to check whether the values in sub array consecutively matches the numbers in big array. For example:
Array 1: 1,3,6,5,4
Array 2: 1,3,6 - true because 1,3 is matching with array 1.

Array 2: 1,6 - false because after 1 there is no 6 in array 1. It should match consecutively.

Array 2: 6,5,4 - true.

Array 2: 5,4,7 - false because is no number 7 after 4 in array 1.
But I am getting true for this last statement. I am unable to figure out why.

Thanks in advance :)


function IsSubArray() {
    inputArray1 = document.getElementById("inputText1").value.split(",");
    inputArray2 = document.getElementById("inputText2").value.split(",");

    var msg = false;
    for (j = 0; j < inputArray2.length - 1; j++) {
        if (j > 0 && msg == false)
            break;
        for (i = 0; i < inputArray1.length - 1; i++) {
            if (inputArray2[j] == inputArray1[i] && inputArray2[j + 1] != inputArray1[i + 1]) {
                msg = false;
                break;
            }
            else if (inputArray2[j] == inputArray1[i] && inputArray2[j + 1] == inputArray1[i + 1]) {
                msg = true;
                break;
            }
        }
    }
    document.getElementById("output").value = msg + " : LongArray: " + inputArray1 + " , ShortArray: " + inputArray2;
}





我的尝试:



到目前为止,一切都有效,除了最后一个声明。



What I have tried:

So far everything works except for the last statement.

推荐答案

这是一个针对你的反手解决方案:



取数字并将它们变成一个字符串,这样就有两个字符串:

'13654'和'136'。



现在,您需要做的就是使用php'find-the-substring'函数来查看第二个字符串是否在第一个字符串中。你仍然应该研究原始路线的逻辑,因为这是一个很好的学习经历。
Here's a back-handed solution for you:

Take the digits and turn them into a character strings so you have two strings:
'13654' and '136'.

Now, all you need do is use the php 'find-the-substring' function to see if the second string is in the first. You should still work on the logic of your original route as it's a good learning experience.


这篇关于Is_sub-array-不返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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