【发布时间】: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