Проблема в
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 +=
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)
Возможно, было бы понятнее использовать массив при проверке нескольких значений (особенно, если вы планируете иметь более двух проверок в конечном итоге):
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)