Home > Net >  JS console.log only words which have more consonants that vowels
JS console.log only words which have more consonants that vowels

Time:12-10

var arr = ['french','normal','chinese','japanese','indian','italian','greek','spanish','mediterranean','lebanese','moroccan','turkish','thai']

Hello, I want to console.log only words which have more consonants in them than vowels using only for loop or/also if else without functions. (without any "#include" and so on)
Thank you!

I'm thinking that we need two more arrays which have vowels and consonants in them, but I'm not sure

var vov = ["a", "e", "i", "o", "u", "y"];
var cons = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"]

CodePudding user response:

No array methods:

const arr = ['french', 'normal', 'chinese', 'japanese', 'indian', 'italian', 'greek', 'spanish', 'mediterranean', 'lebanese', 'moroccan', 'turkish', 'thai']
const vows = ["a", "e", "i", "o", "u", "y"];
const cons = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"]

for (const word of arr) {
  let vowCount = 0
  let conCount = 0
  for (const char of word) {
    if (includes(vows, char)) {
      vowCount  = 1
    } else if (includes(cons, char)) {
      conCount  = 1
    }
  }
  if (conCount > vowCount) {
    console.log(word)
  }
}

function includes(array, char) {
  for (const c of array) {
    if (c === char) return true
  }
  return false
}

Using regex:

const arr = ['french', 'normal', 'chinese', 'japanese', 'indian', 'italian', 'greek', 'spanish', 'mediterranean', 'lebanese', 'moroccan', 'turkish', 'thai']
const vows = ["a", "e", "i", "o", "u", "y"];
const cons = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"]

const consRegexp = new RegExp(cons.join('|'), 'g')
const vowsRegexp = new RegExp(vows.join('|'), 'g')

const output = arr.filter(word => word.match(consRegexp).length > word.match(vowsRegexp).length)
console.log(output)

  • Related