javascript & Uncaught TypeError: arr is not iterable bug All In One
javascript & Uncaught TypeError: arr is not iterable bug All In One
error
function compute(arr) {
const [left, symbol, right] = arr;
switch (symbol) {
case "*":
result = left * right;
break;
case "/":
result = left / right;
break;
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
}
return result;
}
const arr1 = [3, "+"];
const arr2 = [2, "*", 3];
compute(arr1.push(compute(arr2)));
// ❌ Uncaught TypeError: arr is not iterable
const [left, symbol, right] = arr2;
// ✅
compute(arr2);
// 6
arr1.push(6);
// 3, 返回 arr1.length
compute(3);
// ❌ Uncaught TypeError: arr is not iterable
solution
function compute(arr) {
const [left, symbol, right] = arr;
switch (symbol) {
case "*":
result = left * right;
break;
case "/":
result = left / right;
break;
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
}
return result;
}
const arr1 = [3, "+"];
const arr2 = [2, "*", 3];
arr1.push(compute(arr2));
compute(arr1);
What went wrong?
The value which is given as the right-hand side of for...of
, or as argument of a function such as Promise.all
or TypedArray.from
, or as the right-hand side of an array destructuring assignment
, is not an iterable object
.
An iterable can be a built-in iterable type such as Array
, String
or Map
, a generator
result, or an object implementing the iterable protocol
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/is_not_iterable
LeetCode
- Basic Calculator II
https://leetcode.com/problems/basic-calculator-ii/
function calculate(s: string): number {
const symbols = ["*", "/", "+", "-"];
// remove spaces
// s = s.replaceAll(/\s/g, ``);
s = s.replace(/\s/g, ``);
if(![...s].some(i => symbols.includes(i))) {
return parseInt(s);
}
const stack = [];
let temp = ``;
// for(let c of s) {
// if(!symbols.includes(c)) {
// temp += c;
// } else {
// stack.push(parseInt(temp));
// stack.push(c);
// temp = ``;
// }
// if(temp) {
// stack.push(parseInt(temp));
// }
// }
for (let i = 0; i < s.length; i++) {
const c = s[i];
if(!symbols.includes(c)) {
temp += c;
} else {
stack.push(parseInt(temp));
stack.push(c);
temp = ``;
}
if(temp && i === s.length - 1) {
stack.push(parseInt(temp));
}
}
console.log(`stack =`, stack);
let total = 0;
let arr1 = [];
let arr2 = [];
let isMD = false;
for(let i of stack) {
// 运算优先级 +- ? */
if(!symbols.includes(i)) {
if(isMD) {
arr2.push(i);
} else {
arr1.push(i);
}
} else {
if(["+", "-"].includes(i)) {
isMD = false;
arr1.push(i);
}
if(["*", "/"].includes(i)) {
isMD = true;
let left = arr1.pop();
arr2.push(left);
arr2.push(i);
}
}
}
// function compute(arr) {
// const [left, symbol, right] = arr;
// switch (symbol) {
// case "*":
// return left * right;
// case "/":
// return left / right;
// case "+":
// return left + right;
// case "-":
// return left - right;
// // break;
// }
// }
function compute(arr) {
const [left, symbol, right] = arr;
let result = 0;
switch (symbol) {
case "*":
result = left * right;
break;
case "/":
result = Math.floor(left / right);
break;
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
}
return result;
}
console.log(`arr2 =`, arr2);
if(arr1.length && arr2.length) {
arr1.push(compute(arr2));
console.log(`arr1 =`, arr1);
total += compute(arr1);
} else if(arr1.length) {
// ❌
total += compute(arr1);
} else if(arr2.length) {
total += compute(arr2);
}
// total += compute(arr1.push(compute(arr2)));
return total;
};
/*
Line 3: Char 9: error TS2550: Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later.
arr = [3, "+", 2, "*", 3];
"42"
*/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17134081.html
未经授权禁止转载,违者必究!