en.javascript.info/1-js/4-data-structures/3-string/2-check-spam/solution.md
2015-01-11 01:54:57 +03:00

17 lines
568 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Метод `indexOf` ищет совпадение с учетом регистра. То есть, в строке `'xXx'` он не найдет `'XXX'`.
Для проверки приведем к нижнему регистру и строку `str` а затем уже будем искать.
```js
//+ run
function checkSpam(str) {
var lowerStr = str.toLowerCase();
return !!(~lowerStr.indexOf('viagra') || ~lowerStr.indexOf('xxx'));
}
alert( checkSpam('buy ViAgRA now') );
alert( checkSpam('free xxxxx') );
alert( checkSpam("innocent rabbit") );
```