Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Tome el objeto (diccionario) y la cadena (oración) como parámetros de función y devuelva los valores de las claves adecuadas

Tengo que hacer una función que tome el diccionario (objeto) y la oración como parámetros y devuelva el valor de la clave adecuada y si falta una palabra en una función de diccionario debería arrojar un error "Error: valor faltante"

estos son ejemplos de salida:

translate({ "je": "yo", "suis": "soy", "pere": "padre", "ton": "tu"}, "je suis ton pere" ) // 'Yo soy tu padre '

translate({ "the": "le", "cute": "mignon", "your": "ton", "dog": "chien", "is": "est"}, "the dog is cute" ) // 'le chien est mignon'

translate({ "the": "le", "cute": "mignon", "your": "ton", "dog": "chien", "is": "est"}, "el perro es esponjoso" ) // 'Error: valor faltante'

mi código es así: y funciona pero se detiene en el índice 0, por lo que solo puedo obtener el primer resultado, que es "I"

 let dictionary = { "je": "I", "suis": "am", "pere": "father", "ton": "your" } let dictKeys = Object.keys(dictionary) let translated = [] for(let i=0; i < sentence.length; i++){ for(let j=0; j < dictKeys.length; i++){ if(sentence[i] == dictKeys[j]){ translated.push(dictionary[dictKeys[j]]) console.log(translated) } } }

No tengo idea de cómo terminar este ejercicio, por favor ayúdame.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su código estaba casi bien. En su segundo ciclo, incrementó nuevamente i en lugar de j. Agregué un toLowerCase para cubrir palabras sensibles a mayúsculas y minúsculas

 let dictionary = { "je": "I", "suis": "am", "pere": "father", "ton": "your" } let dictKeys = Object.keys(dictionary) let translated = [] let missing = [] let sentence = "Je suis ton ami abc gfukgv".split(" ") for (let i = 0; i < sentence.length; i++) { if (!dictKeys.includes(sentence[i].toLowerCase())) { missing.push(sentence[i]) //console.error(`"${sentence[i]}" word is missing from dictionary`) } for (let j = 0; j < dictKeys.length; j++) { if (sentence[i].toLowerCase() == dictKeys[j].toLowerCase()) { translated.push(dictionary[dictKeys[j]]) } } } missing.length > 0 ? console.error(`The following words are missing from the dictionary: ${missing.join(", ")}`) : null console.log(translated.join(" "))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!