返回課程

檢查垃圾郵件

重要性:5

撰寫一個函式 checkSpam(str),如果 str 包含「viagra」或「XXX」,則傳回 true,否則傳回 false

函式必須不區分大小寫

checkSpam('buy ViAgRA now') == true
checkSpam('free xxxxx') == true
checkSpam("innocent rabbit") == false

開啟一個有測試的沙盒。

為了讓搜尋不區分大小寫,我們將字串轉換成小寫,然後再搜尋

function checkSpam(str) {
  let lowerStr = str.toLowerCase();

  return lowerStr.includes('viagra') || lowerStr.includes('xxx');
}

alert( checkSpam('buy ViAgRA now') );
alert( checkSpam('free xxxxx') );
alert( checkSpam("innocent rabbit") );

在沙盒中開啟有測試的解答。