本文主要是介绍0053【Edabit ★☆☆☆☆☆】【计算投票】Upvotes vs Downvotes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0053【Edabit ★☆☆☆☆☆】【计算投票】Upvotes vs Downvotes
arrays
language_fundamentals
numbers
objects
Instructions
Given an object containing counts of both upvotes and downvotes, return what vote count should be displayed. This is calculated by subtracting the number of downvotes from upvotes.
Examples
getVoteCount({ upvotes: 13, downvotes: 0 }) // 13
getVoteCount({ upvotes: 2, downvotes: 33 }) // -31
getVoteCount({ upvotes: 132, downvotes: 132 }) // 0
Notes
- You can expect only positive integers for vote counts.
Solutions
function getVoteCount(votes) {return votes.upvotes - votes.downvotes;
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertEquals(getVoteCount({ upvotes: 13, downvotes: 0 }), 13)
Test.assertEquals(getVoteCount({ upvotes: 2, downvotes: 33 }), -31)
Test.assertEquals(getVoteCount({ upvotes: 132, downvotes: 132 }), 0)
Test.assertEquals(getVoteCount({ upvotes: 0, downvotes: 0 }), 0)
Test.assertEquals(getVoteCount({ downvotes: 4, upvotes: 1 }), -3)
这篇关于0053【Edabit ★☆☆☆☆☆】【计算投票】Upvotes vs Downvotes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!