Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
A function which returns certain data from a tweet (string)

I have wrote the function below, which takes a parameter (a string) and extracts certain data out of it.

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;
}

The code takes a string and attempts to extract the amount of characters, the number of @ and the number of #. The @ and # contents should then be entered into the respective arrays (in const newObject).

The above code works for strings which contain either @ or # but not for strings which contain both.

Any ideas on how to combine the two.

The tests I have wrote are below, and all except the last one are passing.

    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 Respostas
Responde à pergunta

0

Your function seems to work.

Here is the output we get when we call getTweetData on the string: "Hello @stackoverflow #coding #needhelp"

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

Edit: There is a mistake in your last test: You should call:

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

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda