相互

2016-10-04 68 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);