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

173
Vistas
Una función que devuelve ciertos datos de un tweet (cadena)

He escrito la función a continuación, que toma un parámetro (una cadena) y extrae ciertos datos de él.

 function getTweetData(tweet) { const newObject = { tags: [], mentions: [], tagCount: 0, mentionCount: 0, length: 0, }; //length newObject.length = tweet.length; const findMention = tweet.split("@").length - 1; newObject.mentionCount = findMention; const atRegex = tweet.match(/@(.[a-z0-9]+)/g); newObject.mentions = atRegex === null ? [] : atRegex; const findHashtag = tweet.split("#").length - 1; newObject.tagCount = findHashtag; const regex = tweet.match(/#(.[a-z0-9]+)/g); newObject.tags = regex === null ? [] : regex; //need to put the above two together console.log(newObject); return newObject; }

El código toma una cadena e intenta extraer la cantidad de caracteres, el número de @ y el número de #. Los contenidos @ y # luego deben ingresarse en las matrices respectivas (en const newObject).

El código anterior funciona para cadenas que contienen @ o #, pero no para cadenas que contienen ambos.

Cualquier idea sobre cómo combinar los dos.

Las pruebas que he escrito están a continuación, y todas excepto la última están pasando.

 const getTweetData = require("../get-tweet-data"); describe("Name of the group", () => { test("should return return empty object", () => { expect(typeof getTweetData(" ")).toBe("object"); }); test("should return length of character tweets", () => { expect(getTweetData("tweet")).toEqual({ tags: [], mentions: [], tagCount: 0, mentionCount: 0, length: 5, }); }); test("should return amount of hashtags mentioned in tweet", () => { expect(getTweetData("tweet #coding")).toEqual({ tags: ["#coding"], mentions: [], tagCount: 1, mentionCount: 0, length: 13, }); }); test("should return amount of @mentions mentioned in tweet", () => { expect(getTweetData("tweet @coders")).toEqual({ tags: [], mentions: ["@coders"], tagCount: 0, mentionCount: 1, length: 18, }); }); test("should return amount of @mentions and #hashtags mentioned in tweet", () => { expect("Hello @stackoverflow #coding #needhelp").toEqual({ tags: ["#coding", "#needhelp"], mentions: ["@stackoverflow"], tagCount: 2, mentionCount: 1, length: 38, }); }); });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Su función parece funcionar.

Este es el resultado que obtenemos cuando llamamos a getTweetData en la cadena: "Hola @stackoverflow #codificación #necesitoayuda"

 { tags: [ '#coding', '#needhelp' ], mentions: [ '@stackoverflow' ], tagCount: 2, mentionCount: 1, length: 38 }

Editar : hay un error en su última prueba: debe llamar a:

expect(getTweetData("Hello @stackoverflow #coding #needhelp")).toEqual({ ...

en lugar de expect("Hello @stackoverflow #coding #needhelp").toEqual({ ...

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