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

Multiple OR operators in ternary condition,

我正在尝试通过检查字符的值并根据三元运算符的真或假评估替换它来创建新字符串。

我只使用一个字符就成功了,从我读到的内容来看,三元运算符条件可能包括 ||或操作员。我试过只使用两个,但没有产生正确的结果。

是不是因为条件一满足就过不了了||或运算符?

三元条件可以包含多少,将条件放入变量或函数中会更好吗?

我知道这个问题可以用不同的方式解决,但我正在尝试使用三元运算符以获得更好的理解。

提前致谢,我是 JavsScript 新手。

.

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. 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'

问题是

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

这个条件 总是 为真 - 如果 cond 是 space,它将满足 cond != 'a'。如果cond'a',则满足cond != ' '。如果 cond 是其他任何东西,它将满足 cond != ' '.

改为使用:

(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 U.S. 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'

就是说,您 真的 不应该滥用条件运算符来替代 if-else:

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. 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)

如果你想在这里使用条件运算符,你应该在 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 U.S. 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)

当有多个值要检查时使用数组可能会更清楚(特别是如果您计划最终进行超过 2 次检查):

let input = 'President Donald Trump signed an executive order on Friday aimed at preventing counterfeit products from abroad from being sold to U.S. 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)