javascript - 为什么我的 array.forEach() 没有中断迭代并返回“true”?
问题描述
我正在尝试编写一个函数来比较数组中的值以查看它们是否与常量“flightValue”匹配。然而,即使它返回一个“真”值,循环也不会停止并最终返回一个未定义的值。
我的逻辑在这里有什么突破?
// Write a function that takes an integer flightLength (in minutes) and an array of integers movieLengths (in minutes) and returns a boolean indicating whether there are two numbers in movieLengths whose sum equals flightLength.
let moviesArray = [75, 120, 65, 140, 80, 95, 45, 72];
function canWatchTwoMovies(flightLength, array) {
let startIndex = 0;
let endIndex = array.length;
array.forEach((el, index)=> {
let currentPointer = index;
for(i = currentPointer+1; i < endIndex; i++) {
if(array[currentPointer] + array[i] == flightLength) {
// Uncomment the console.log to see that there is a 'true' value
// console.log(array[currentPointer] + array[i] == flightLength);
// Why is this not breaking the loop and returning "true"?
return true;
}
}
});
// I have commented out the default return value
// return false;
console.log("The function doesn't break and always runs to this point");
}
// Should return 'true' for addition of first two elements '75' and '120'
canWatchTwoMovies(195, moviesArray);
编辑:以下是 Maor Refaeli 回复的清理代码:
function canWatchTwoMovies(flightLength, array) {
for (let index = 0; index < array.length; index++) {
let currentPointer = index;
for(let i = currentPointer+1; i < array.length; i++) {
if(array[currentPointer] + array[i] == flightLength) {
return true;
}
}
}
return false;
}
解决方案
使用时,forEach
您声明了一个将为数组中的每个项目执行的函数。
实现您想要的一种方法是使用 for 循环:
// Write a function that takes an integer flightLength (in minutes) and an array of integers movieLengths (in minutes) and returns a boolean indicating whether there are two numbers in movieLengths whose sum equals flightLength.
let moviesArray = [75, 120, 65, 140, 80, 95, 45, 72];
function canWatchTwoMovies(flightLength, array) {
let startIndex = 0;
let endIndex = array.length;
for (let index = 0; index < array.length; i++) {
let el = array[index];
let currentPointer = index;
for(i = currentPointer+1; i < endIndex; i++) {
if(array[currentPointer] + array[i] == flightLength) {
// Uncomment the console.log to see that there is a 'true' value
// console.log(array[currentPointer] + array[i] == flightLength);
// Why is this not breaking the loop and returning "true"?
return true;
}
}
}
// I have commented out the default return value
// return false;
console.log("The function doesn't break and always runs to this point");
}
// Should return 'true' for addition of first two elements '75' and '120'
canWatchTwoMovies(195, moviesArray);
推荐阅读
- javascript - using () 强制 JavaScript 解释器首先评估
- sqlite - 如何使用单个脚本自动打开 sqlite3.exe shell 并运行命令?
- mysql - 如何在 MySQL 表中保留 ID 范围
- android - 在 Fragment 中构造 ViewModelProvider 时,应该传入 Fragment 还是 Activity?
- c++ - 是否为 MSVC 编译器预定义了 __CHAR_BIT__ 宏?
- python - 使用 shapley 进行线性回归时出错:TypeError: 'Lasso' object is not callable
- python - 当有一些字符串Python时删除div
- java - Twilio - Speech To Text 缺少收集 SpeechResult
- php - 创建日志脚本
- python - 如何克服“错误:在注册表中找不到对象'jupyter.widget'”?