【发布时间】:2021-09-14 21:46:21
【问题描述】:
我正在尝试比较两个数组(包含3个整数)并返回一个符合逻辑的提示消息数组
-当数组中的1位数字和位置匹配时,推送“几乎”
-当 1 位匹配但位置不同时推送“不完全”
-当没有数字匹配时推送“不正确”
- 完全匹配时按“正确”
数组示例:
Array1 = [2,7,6]
ReferenceArray= [2,9,7]
Hint= [“Almost”, “Not Quite”];
到目前为止我的代码:
function check( array1, referenceArray ) {
let hint=[];
for(i=0;i<referenceArray.length;i++){
for (j=0;j<Array1.length;j++){
//value and position match
if ((referenceArray[i] && reference.indexOf[i]) === (Array1[j] && Array1.indexOf[j])) {
return hint.push('almost');
}
//value matches but not position
else if(( referenceArray[i] ===Array1[j]) && !(referenceArray.indexOf[i]===Array1.indexOf[j] )){
return hint.push('not quite');
}
}// end of Array1 iteration
} // end of reference interation
// if all values and position match
if(referenceArray===Array1){
return hint.push("correct");
}
//if no values match
else if (referenceArray!==Array1){
return hintArray.push("incorrect");
}
【问题讨论】:
标签: javascript arrays logic