series
series

Reputation: 3

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.

.

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'

Upvotes: 0

Views: 493

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370979

The problem is

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

This condition will always be true - if cond is a space, it will fulfill cond != 'a'. If cond is 'a', it will fulfill cond != ' '. If cond is anything else, it will fulfill 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 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'

That said, you really really shouldn't abuse the conditional operator as a replacement for 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)

If you want to use the conditional operator here, you should do it after the news += part:

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)

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):

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)

Upvotes: 2

Related Questions