相互

2016-10-04 66 views
0

比较数组元素我有一个字符数组:相互

var charCode = [97,98,100,101,103]; 

我想遍历这个数组,并比较charCode[i]charCode[i+1]所以比较:

charCode[0] with charCode[1] 
charCode[1] with charCode[2] 
charCode[2] with charCode[3] 
charCode[3] with charCode[4] 

我也想检查charCode[i] +1 == charCode[i+1]所以这意味着我想看看下一个元素是否比上一个因子大一。还有一件事,如果charCode [3]不遵循这个规则,我想将元素的索引存储在一个单独的变量中。

function fearNotLetter(str) { 
    var bool ; 
    var charCode = []; 
    for (var i = 0; i < str.length; i++) { 
    charCode[i] = str.charCodeAt(i); 
    // charCode = [97,98,99,100,101,103] 
    } 
    for (var n = 0; n < charCode.length; n++) { 
    /* 
    here I'm comparing every element with every other element, 
    which is obviously not what I want 
    */ 
    for (var j = n+1; j < charCode.length; j++) { 
     if (charCode[n] + 1 < charCode[j]) { 
     // don't know what to do here 
     } 
    } 
    } 
    return charCode; 
} 
fearNotLetter("abcdeg"); 
+0

你在你的代码做了一些不同的东西。你正在对阵列的其余部分迭代数组的元素n即(0对1,2,3,4,5 ....) –

回答

1

您可以使用array.reduce和不匹配,则可以将该值推到中间阵列

function fearNotLetter(str) { 
 
    var charCode = []; 
 
    str.split('').reduce(function(p,c){ 
 
    var code_p = p.charCodeAt() 
 
    var code_c = c.charCodeAt() 
 
    if(code_c-code_p !== 1) 
 
     charCode.push(code_p) 
 
    return c 
 
    }) 
 
    return charCode; 
 
} 
 

 
console.log(fearNotLetter('abcdeg'))

+0

感谢您的答案,但此代码返回[98,101]这不是什么我想 – dadadodo

+0

自103-101 = 2,这意味着在101之后有一个值缺失(102),所以它应该返回101 – dadadodo

+0

你也缺少'99',所以即使它应该被突出显示。对?所以你的最终输出将变成:'[98,101]' – Rajesh

0

你只需要一个正常的循环,只是不要去自检查后的最后一个项目[i+1]

var charCode = [97,98,100,101,103]; 
 
// Loop from 0 to n-2. 
 
for (var i = 0; i < charCode.length - 1; i++) { 
 
    if(charCode[i] +1 == charCode[i+1]) 
 
    console.log(charCode[i] +1 + '==' + charCode[i+1]); 
 
}

0

这是一些修改。你正在运行你不需要的内部循环。

function fearNotLetter(str) { 
 
    var bool ; // not sure what this is for 
 
    // you can borrow map from array instead of looping 
 
    var charCode = Array.prototype.map.call(str, str => str.charCodeAt(0)) 
 
    // loop over the characters. I am starting at 1 so we stop when we get to the end 
 
    // you can access the first index by subtracting 1 from ii 
 
    for (var ii = 1; ii < charCode.length; ii++) { 
 
    if (charCode[ii - 1] === charCode[ii]) { 
 
     console.log('samsies', ii - 1, char(charCode[ii]), char(charCode[ii])) 
 
    } 
 
    else if (charCode[ii-1] + 1 === charCode[ii]) { 
 
     console.log('increment yo', ii - 1, char(charCode[ii -1]), char(charCode[ii])) 
 
    } 
 
    } 
 
    return charCode; 
 
} 
 
console.log(
 
    fearNotLetter("abcddeg") 
 
) 
 
// helper function 
 
function char(code) { 
 
    return String.fromCharCode(code) 
 
}

0

你想失败的指标?

function fearNotLetter(str) { 
 
\t var failedElIndexes = []; 
 
\t var j = 0; 
 
\t for(var i = 0 in str){ 
 
\t \t if(i < (str.length-1)){ 
 
\t \t \t var n = str[i]; 
 
\t \t \t var m = str[++i]; 
 
\t \t \t if(String.fromCharCode(n.charCodeAt() + 1) != String.fromCharCode(m.charCodeAt())) { 
 
\t \t \t \t console.log(i + ": " + str[i]); 
 
\t \t \t \t failedElIndexes[j] = i; 
 
\t \t \t \t j++; 
 
\t \t \t } \t 
 
\t \t }else{ 
 
\t \t \t break; 
 
\t \t } 
 

 
\t } 
 
\t return failedElIndexes; 
 
} 
 

 
var otherStr = "abcdeg"; 
 
var myErrIndexes = fearNotLetter(otherStr);