为什么有些值通过 OR 运算符,而其他值则不传递
Why do some values pass the OR operator, and other do not?
在我的
代码中,我试图根据句子使用的字符来检查句子是莫尔斯语还是英语。如果是莫尔斯,解码运行。如果是英语,则运行编码函数。出于某种原因,某些单词(如"朋友"和"鸡")显示为莫尔斯,但"开始"和"斑马"等单词则不显示。为什么会这样?
var input, output, startType;
var inputArray = [];
input = (prompt('Enter the word(s) or sentence(s) that you wish to convert.')).toLowerCase();
function checkInput(input) {
var i, il;
for (i = 0, il = input.length; i < il; i++) {
if (input.charAt(i) === ('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z')) {
startType = 'normal';
break;
}
else {
startType = 'morse';
console.log(i);
}
}
}
checkInput(input);
function Encode(input) {
var i, il;
for (i = 0, il = input.length; i < il; i++) {
if (input.charAt(i) === 'a') {
inputArray.push('.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'b') {
inputArray.push('-...');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'c') {
inputArray.push('-.-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'd') {
inputArray.push('-..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'e') {
inputArray.push('.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'f') {
inputArray.push('..-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'g') {
inputArray.push('--.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'h') {
inputArray.push('....');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'i') {
inputArray.push('..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'j') {
inputArray.push('.---');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'k') {
inputArray.push('-.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'l') {
inputArray.push('.-..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'm') {
inputArray.push('--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'n') {
inputArray.push('-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'o') {
inputArray.push('---');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'p') {
inputArray.push('.--.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'q') {
inputArray.push('--.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'r') {
inputArray.push('.-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 's') {
inputArray.push('...');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 't') {
inputArray.push('-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'u') {
inputArray.push('..-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'v') {
inputArray.push('...-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'w') {
inputArray.push('.--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'x') {
inputArray.push('-..-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'y') {
inputArray.push('-.--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === 'z') {
inputArray.push('--..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '0') {
inputArray.push('-----');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '1') {
inputArray.push('.----');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '2') {
inputArray.push('..---');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '3') {
inputArray.push('...--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '4') {
inputArray.push('....-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '5') {
inputArray.push('.....');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '6') {
inputArray.push('-....');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '7') {
inputArray.push('--...');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '8') {
inputArray.push('---..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '9') {
inputArray.push('----.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '.') {
inputArray.push('.-.-.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '?') {
inputArray.push('..--..');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '!') {
inputArray.push('-.-.--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === ('(')) {
inputArray.push('-.--.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === (')')) {
inputArray.push('-.--.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === ':') {
inputArray.push('---...');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '=') {
inputArray.push('-...-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '-') {
inputArray.push('-....-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '"') {
inputArray.push('.-..-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === ',') {
inputArray.push('--..--');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === "'") {
inputArray.push('.----.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '/') {
inputArray.push('-..-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '&') {
inputArray.push('.-...');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === ';') {
inputArray.push('-.-.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '_') {
inputArray.push('..--.-');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === '@') {
inputArray.push('.--.-.');
if (input.charAt(i + 1) != ' ') {
inputArray.push('/');
}
}
else if(input.charAt(i) === ' ') {
inputArray.push('//');
}
else {
inputArray.push(input.charAt(i));
}
}
}
switch(startType) {
case 'normal':
Encode(input);
output = inputArray.join('');
break;
case 'morse':
Decode(input);
output = inputArray.join('');
break;
default:
output = 'Unrecognized language!';
break;
}
console.log('Input was: ' + input);
console.log('Output is: ' + output);
console.log('Start type was: ' + startType);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
因为('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z')
总是会返回a
所以如果你的字符串中有a
,那么它将被视为normal
类型。
如果您只想检查input
是否只有字母[a-z
]个字符,则可以使用
function checkInput(input) {
startType = /^[a-z]+$/.test(input) ? 'normal' : 'morse';
console.log(startType)
}
您是否尝试过删除字符选择周围的括号?
if (input.charAt(i) === 'a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z')
请改用这个:
if (['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'].indexOf(input.charAt(i)) > -1)
很抱歉在那里误导你!
相关文章:
- 这是什么 ==- javascript 运算符
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- jQuery/JS包含运算符或类似运算符
- 除修剪外的其他功能
- Javascript逻辑运算符和字符串/数字
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- Array.length似乎不起作用;console.log则显示其他情况
- 如何消除代码中的未定义和其他问题
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 模运算符前的双水平线
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- Jquery未定义函数正在停止其他操作
- 正则表达式删除 - 除非前面有其他运算符
- 为什么有些值通过 OR 运算符,而其他值则不传递
- 立即/自执行函数语法:括号与否定运算符(和其他运算符)