简体   繁体   English

三元条件下的多个 OR 运算符,

[英]Multiple OR operators in ternary condition,

I'm trying to create a new string by checking the value of a character and replacing it based on the true or false evaluation from the ternary operator.我试图通过检查字符的值并根据三元运算符的真假评估替换它来创建一个新字符串。

I have had success using only a single character and from what I have read it is possible to have a ternary operator condition include the ||我只使用一个字符就取得了成功,从我读过的内容来看,三元运算符条件可能包括 || or operator.或运营商。 I have tried using only two and it's not producing the correct result.我试过只使用两个,但没有产生正确的结果。

Is it because once the condition is met is wont go past ||是不是因为一旦满足条件就不会过去了 || or operator?或运营商?

How much can ternary conditions contain and would it work better to put conditions into a variable or function?三元条件可以包含多少,将条件放入变量或函数中会更好吗?

I'm aware the problem can be solved differently but I am experimenting with the ternary operator to gain a better understanding.我知道这个问题可以用不同的方式解决,但我正在试验三元运算符以获得更好的理解。

Thanks in advance, I'm new to JavsScript.提前致谢,我是 JavsScript 的新手。

. .

 let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to US citizens who shop online using Amazon.com, Walmart.com or other e-commerce websites, the White House said.' const vowels = ['a', 'e', 'i', 'o', 'u'] const ranNum = () => {return Math.floor(Math.random() * 5)} let news = '' const swapper = input => { let count = 0 while (count != input.length) { let cond = input[count].toLowerCase() cond != ' ' || cond != 'a' ? news += vowels[ranNum()] : news += input[count] count ++ } console.log(news) } console.log(input) swapper(input) //c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u'

The problem is问题是

cond != ' ' || cond != 'a' ? (...)

This condition will always be true - if cond is a space, it will fulfill cond != 'a' .此条件将始终为真 - 如果cond是一个空格,它将满足cond != 'a' If cond is 'a' , it will fulfill cond != ' ' .如果cond'a' ,它将满足cond != ' ' If cond is anything else, it will fulfill cond != ' ' .如果cond是其他任何东西,它将满足cond != ' '

Instead use:而是使用:

(cond === ' ' || cond === 'a') ? news += input[count] : news += vowels[ranNum()];

 let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to US citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.' const vowels = ['a', 'e', 'i', 'o', 'u'] const ranNum = () => {return Math.floor(Math.random() * 5)} let news = '' const swapper = input => { let count = 0 while (count != input.length) { let cond = input[count].toLowerCase(); (cond === ' ' || cond === 'a') ? news += input[count] : news += vowels[ranNum()]; count ++ } console.log(news) } console.log(input) swapper(input) //c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u'

That said, you really really shouldn't abuse the conditional operator as a replacement for if - else :也就是说,你真的不应该滥用条件运算符来代替if - else

 let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to US citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.' const vowels = ['a', 'e', 'i', 'o', 'u'] const ranNum = () => { return Math.floor(Math.random() * 5) } let news = '' const swapper = input => { let count = 0 while (count != input.length) { let cond = input[count].toLowerCase(); if (cond === ' ' || cond === 'a') { news += input[count] } else { news += vowels[ranNum()]; } count++ } console.log(news) } console.log(input) swapper(input)

If you want to use the conditional operator here, you should do it after the news += part:如果你想在这里使用条件运算符,你应该在以后news +=部分:

news += (cond === ' ' || cond === 'a') ? input[count] : vowels[ranNum()];

 let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to US citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.' const vowels = ['a', 'e', 'i', 'o', 'u'] const ranNum = () => { return Math.floor(Math.random() * 5) } let news = '' const swapper = input => { let count = 0 while (count != input.length) { let cond = input[count].toLowerCase(); news += (cond === ' ' || cond === 'a') ? input[count] : vowels[ranNum()]; count++ } console.log(news) } console.log(input) swapper(input)

It might be clearer to use an array when there are multiple values to check (especially if you plan on having more than 2 checks eventually):当有多个值要检查时,使用数组可能更清楚(特别是如果您计划最终进行 2 个以上的检查):

 let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to US citizens who shop online using Amazon.com , Walmart.com or other ecommerce websites, the White House said.' const vowels = ['a', 'e', 'i', 'o', 'u'] const ranNum = () => { return Math.floor(Math.random() * 5) } let news = '' const swapper = input => { let count = 0 while (count != input.length) { let cond = input[count].toLowerCase(); news += [' ', 'a'].includes(cond) ? input[count] : vowels[ranNum()]; count++ } console.log(news) } console.log(input) swapper(input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM