我是编程新手,现在需要回答以下问题:
创建一个函数,接收一个字符串参数,该字符串将包含一些类似 Twitter 中的提及和标签。
E.g. "So excited to start @coding on Monday! #learntocode #codingbootcamp"
该函数应返回一个描述发现的哈希标签和提及数量的对象:
{ hashtags: 2, mentions: 1 }
我创建的代码如下:
我创建的代码如下:
function countHashtagsAndMentions(str) {
let split = str.split(" ");
let count = 0
for (let i = 9; i < str.length; i++) {
let hash = split.filter(hashtag => hashtag.match(/#/g))
if (hash === 1) {
count ++
}
let total = {
hash = count,
}
return hash
}
}
我的代码正在运行这个;
describe("countHashtagsAndMentions", () => {
it("returns an object", () => {
expect(typeof countHashtagsAndMentions("")).to.equal("object");
});
it("returns {hashtags: 0, mentions: 0} if it finds none", () => {
expect(
countHashtagsAndMentions(
"hello this is a tweet guaranteed to get very little engagement"
)
).to.eql({ hashtags: 0, mentions: 0 });
});
it("recognises no mentions", () => {
expect(countHashtagsAndMentions("#yolo")).to.eql({
hashtags: 1,
mentions: 0
});
});
it("recognises no hashtags", () => {
expect(countHashtagsAndMentions("@yobo")).to.eql({
hashtags: 0,
mentions: 1
});
});
it("finds multiple hashtags and mentions and returns that number", () => {
expect(countHashtagsAndMentions("#yolo @bolo #golo")).to.eql({
hashtags: 2,
mentions: 1
});
expect(countHashtagsAndMentions("@boyo #goyo @loyo #zoyo")).to.eql({
hashtags: 2,
mentions: 2
});
expect(
countHashtagsAndMentions(
'"So excited to start at @northcoders on Monday! #learntocode #codingbootcamp"'
)
).to.eql({ hashtags: 2, mentions: 1 });
});
});
大家有没有什么建议可以让我的代码正常工作?