2 回答

TA貢獻1809條經驗 獲得超8個贊
我會在函數外構建refuse和數組并將它們作為參數傳遞。args
const refuse = [">", ";", "&", ","]
const args = [">>>>", ";;;;;;", "&&", ",,"]
function checkIllegal(refuse, args) {
let illegal = false;
refuse.forEach(e => {
args.forEach(string => {
if (string.includes(e)) illegal = true;
console.log("Blacklisted");
});
});
return illegal;
}
console.log(checkIllegal(refuse, args));
這仍然基于整個數組而不是每個字符串返回 true 或 false ,這是你需要的嗎?
否則我不會在函數內部而是在函數外部循環遍歷 args,然后您可以檢查每個字符串。

TA貢獻1943條經驗 獲得超7個贊
https://jsfiddle.net/x24qnes6/
以下解決方案適用于單個字符和多個字符
function isRefused() {
const refuse = ">,>>,>,&,|,;,".split(',')
const args = "; ;; | > << >>".trim().split(/ +/g);
let illegal = false;
refuse.forEach(r => {
args.forEach(a => {
if (a.includes(r)) {
console.log(`${a} is blacklisted`)
illegal = true;
}
})
})
return illegal;
}
console.log(`Blacklisted? ${isRefused()}`)
你必須反過來檢查。args[j]
有沒有refused[i]
然而,更好的方法是為此使用正則表達式。
添加回答
舉報