簡體   English   中英

三元條件下的多個 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 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'

問題是

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 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'

也就是說,你真的不應該濫用條件運算符來代替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)

如果你想在這里使用條件運算符,你應該在以后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)

當有多個值要檢查時,使用數組可能更清楚(特別是如果您計划最終進行 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