【发布时间】:2020-11-23 18:00:57
【问题描述】:
假设我要搜索ticks数组并返回数组中以S开头的所有项目,然后将它们写入sCompanies = []。
有人知道我如何使用 for 或 while 循环来解决这个问题吗?
// Iterate through this list of tickers to build your new array:
let tickers = ['A', 'SAS', 'SADS' 'ZUMZ'];
//console.log(tickers);
// Define your empty sCompanies array here:
//Maybe need to use const sComapnies = [] ?
let sCompanies = []
// Write your loop here:
for (i = 0; i < tickers.length; i++) {
console.log(tickers[i]);
}
// Define sLength here:
sLength = 'test';
/*
// These lines will log your new array and its length to the console:
console.log(sCompanies);
console.log(sLength);*/
【问题讨论】:
-
这个问题与许多其他关于 SO 上的 JavaScript 数组的问题非常相似。你尝试过什么来实现你的目标?我没有看到任何代码可以做到这一点。
-
您特别要求提供涉及“for 或 while 循环”的解决方案。这是(似乎是)家庭作业的规定要求吗?从更大的角度来看,您似乎没有尝试自己解决此问题。对于未来的问题,您应该始终这样做,因为 Stack Overflow 不是代码编写或家庭作业解决服务。
标签: javascript arrays string loops search