Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

579
Vistas
Javanais // JavaScript

I'm trying to add 'av' between consonant and vowel, but only if after one consonant I have one vowel.

Example:

'Hello all' will be something like 'Havellavo all'.

I have tried this code:

let text =
  "Il vous faut pour cet exercice, traduire le texte suivant en Javanais. Pour se faire, vous devez intégrer 'av' après chaque consonne suivi d'une voyelle";

let voyelle = /^[aeiou]$/;

const translate = (str) => {
  let newStr = "";

  for (let i = 0; i < str.length; i++) {
    if (str[i] !== voyelle) {
      newStr += str[i];
      if (str[i + 1] === voyelle) {
        newStr += "av";
      }
    }
  }
  return newStr;
};


console.log(translate(text));

This is not working and I don't know why. Can anyone give me some tips or give me one solution with the explanation?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I would do it this way :

  • Looping through all char and for each char, check if the current one is a vowel and if the previous one is a consonant

This would look something like this.

const vowels = ['a', 'e', 'i', 'o', 'u', 'y']

// splitting text by char

function translate(text) {
  const chars = text.split('')

  for (let i = 1; i < chars.length; i++) {
    if (vowels.includes(chars[i]) && chars[i - 1] !== " " && !vowels.includes(chars[i - 1])) {
      chars[i] = "av" + chars[i]
    }
  }

  return chars.join('')
}

console.log(translate('Hello world'))
console.log(translate('Hello all'))

Note : Here the uppercase won't work, it will only work with lower case vowels


In your case you were missing the String#includes method that checks if a char is in a string. str[i] !== voyelle couldn't work since your were comparing a char and an array

about 4 years ago · Juan Pablo Isaza Denunciar

0

Alternatively you could use the power of regular expressions combined with the replace method:

const regex = /([bcdfghjklmnpqrstvwxz])([aeiouy])/gi;

const translate = (text) => text.replace(regex, "$1av$2");

console.log(translate('Hello world'))
console.log(translate('Hello all'))

The regular expression matches a consonant followed by a vowel. The replacement expression references this consonant with $1 -- since it is captured in the first capture group (cf. parentheses in the regular expression) -- and the vowel with $2. So for instance, He is matched and replaced by Have ($1 is H and $2 is e) and lo is matched and replaced by lavo.

about 4 years ago · Juan Pablo Isaza Denunciar

0

  • Based on your criteria, you can use regular expression assertions to find the vowel that comes after a consonant (Lookbehind assertion), and isn't before another vowel (Negative lookahead assertion).
  • Next, just grab all the indices that these occur.
  • Split your string into an array, and loop over the indices in reverse order and insert the 'av' string at the index positions. We go in reverse order so the index values won't change as the array grows.
  • Then, join everything at the end.

const translate = (str) => {
    let regex = /(?<=[bcdfghjklmnpqrstvwxyz])[aeiou](?![aeiou])/gi;
    let match;
    let indices = [];
    while ((match = regex.exec(str))) {
        indices.push(match.index);
    }
    // Store indices in reversed order, highest to lowest
    indices.reverse();
    let strarr = str.split("");
    for (let index of indices) {
        // Insert 'av' at each index, starting from the end so the earlier indices don't change
        strarr.splice(index, 0, "av");
    }

    return strarr.join("");
}
console.log(translate('Hello all'));
console.log(translate("Il vous faut pour cet exercice, traduire le texte suivant en Javanais. Pour se faire, vous devez intégrer 'av' après chaque consonne suivi d'une voyelle"));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda