【问题标题】:Multiple OR operators in ternary condition,三元条件下的多个 OR 运算符,
【发布时间】:2020-05-18 07:17:32
【问题描述】:

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

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

是不是因为条件一旦满足就不会过去了||还是运营商?

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

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

提前致谢,我是 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'

【问题讨论】:

  • 你不能像那样使用三元。它现在作为短路工作

标签: javascript operator-precedence conditional-operator or-operator


【解决方案1】:

问题是

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

这个条件总是为真 - 如果cond 是一个空格,它将满足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)

【讨论】:

  • 当比较多个值时,我已经习惯使用这种“模式”:news += ([' ', 'a'].includes(cond)) ? input[count] : vowels[ranNum()];。使其更具可读性,易于添加/删除比较值,并且更简洁。如果是简单字母,您也可以使用字符串而不是数组:news += (' a'.includes(cond)) ? input[count] : vowels[ranNum()];
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 2013-06-06
  • 2011-06-26
  • 2013-01-14
  • 2015-08-29
  • 2016-11-02
  • 2020-01-18
  • 2018-03-06
相关资源
最近更新 更多