- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
描述
Middle Earth is about to go to war. The forces of good will have many battles with the forces of evil. Different races will certainly be involved. Each race has a certain worth when battling against others. On the side of good we have the following races, with their associated worth:
在邪恶方面,我们有:
Although weather, location, supplies and valor play a part in any battle, if you add up the worth of the side of good and compare it with the worth of the side of evil, the side with the larger worth will tend to win.
Thus, given the count of each of the races on the side of good, followed by the count of each of the races on the side of evil, determine which side wins.
输入
The function will be given two parameters. Each parameter will be a string separated by a single space. Each string will contain the count of each race on the side of good and evil.
The first parameter will contain the count of each race on the side of good in the following order:
霍比特人、人类、 Sprite 、矮人、老鹰、巫师。
The second parameter will contain the count of each race on the side of evil in the following order:
兽人、人类、座狼、哥布林、乌鲁克海、巨魔、巫师。
All values are non-negative integers. The resulting sum of the worth for each side will not exceed the limit of a 32-bit integer.
输出
Return "Battle Result: Good triumphs over Evil" if good wins, "Battle Result: Evil eradicates all trace of Good" if evil wins, or "Battle Result: No victor on this battle field" if it ends in a tie.
尝试
function goodVsEvil(good, evil){
good = good.split('')
evil = evil.split('')
let light = good.reduce((a, b) => a + b, 0);
let dark = evil.reduce((a, b) => a + b, 0);
if(light > dark){
return "Battle Result: Good triumphs over Evil"
} else if (light < dark){
return "Battle Result: Evil eradicates all traces of Good"
}
return "Battle result: No victor on this battle field"
};
测试结果
Test.expect( goodVsEvil('1 1 1 1 1 1', '1 1 1 1 1 1 1') === 'Battle Result: Evil eradicates all trace of Good', 'Evil should win' );
Test.expect( goodVsEvil('0 0 0 0 0 10', '0 1 1 1 1 0 0') === 'Battle Result: Good triumphs over Evil', 'Good should win' );
Test.expect( goodVsEvil('1 0 0 0 0 0', '1 0 0 0 0 0 0') === 'Battle Result: No victor on this battle field', 'Should be a tie' );
我的所有测试都失败了,我错过了什么?
最佳答案
关于测试失败的原因,您有两个问题:
''
进行拆分,而不是 ' '
,后者基本上是在每个字符后进行拆分。'0 0 0 0 0 10'.split('')
// Result:
[
'0', ' ', '0', ' ',
'0', ' ', '0', ' ',
'0', ' ', '1', '0'
]
相反,你想用一个空格分割,那么结果是这样的:
'0 0 0 0 0 10'.split(' ')
// Result:
[ '0', '0', '0', '0', '0', '10' ]
+
添加字符串将它们连接起来。将会发生的情况的示例:
'0 1 1 1 1 0 0'.split(' ').reduce((a, b) => a + b, 0)
'00111100'
相反,您希望首先将数组项映射到数字。您可以使用 .map(x => Number(x))
或方便地使用 .map(Number)
来完成此操作:
'0 1 1 1 1 0 0'.split(' ').map(Number)
// Result:
[
0, 1, 1, 1,
1, 0, 0
]
// Note how the array items are now numbers and not strings (no quotes in the output)!
然后,将它们添加在一起即可按预期工作:
'0 1 1 1 1 0 0'.split(' ').map(Number).reduce((a, b) => a + b, 0)
// Result:
4
<小时/>要修复代码,您必须替换此...
good = good.split('')
evil = evil.split('')
...这样:
good = good.split(' ').map(Number)
evil = evil.split(' ').map(Number)
此外,我建议查看 how to use a debugger 。这将允许您逐行逐步执行代码并检查值,例如代码的执行情况。这将有助于更好地洞察问题。
<小时/>这刚刚修复了您的测试。我认为与您描述的代码应该做什么相比,这里还缺少另一个步骤,那就是为您的输入赋予不同的“权重”。现在您只需将数字相加,但描述表明您得到的是战斗机数量,而不是它们的加权值作为输入。所以我假设您还必须添加权重列表并相乘:
const weights = {
good: [1, 2, 3, 3, 4, 10],
evil: [1, 2, 2, 2, 3, 5, 10]
}
// ...later on:
good = good.split(' ').map((val, index) => val * weights.good[index])
evil = evil.split(' ').map((val, index) => val * weights.evil[index])
// Note I don't use `Number` anymore because the multiplication (unlike the
// addition!) implicitly converts to a number.
此代码的工作原理是在数组中指定权重列表(实际上是两个,一个代表善,一个代表恶),然后将数字映射到其值乘以相应的权重。第二个参数作为数组索引传递给 map
回调,因此我们可以使用它从权重数组中获取正确的权重。
关于javascript - 善与恶——操纵弦乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60920568/
我是puppeteer的新手(通常对javascript不太了解),并且正在尝试编写一些基本功能来: 从XPath获取所有链接 循环浏览并单击那些链接 屏幕截图并保存页面的HTML 返回屏幕快照,然后
您好,我正在尝试创建一个我想通过网络发送到服务器的数据包,我几乎已经对数据包进行了排序,但是它缺少一个长度标识符,我需要在我的方法结束时计算它并添加放入数据包。 数据包结构是这样的 前导值标识符 (U
我正在尝试更改按钮的样式(实际上只是一个 div)。我可以在 CSS 中使用 .classname:active 来实现,但是按钮只会在被点击时改变样式;单击释放后,它不会保持样式更改。所以,我决定使
我有一个下拉导航栏(fiddle here 和下面的代码片段)。单击 SubItem 时,JS 函数会进行 Ajax 调用(未显示)。我想要实现的是在单击 SubItem 时隐藏 SubItems 列
给定一组平面图(Autocad、svg 或任何需要的格式...),我想以编程方式生成从 A 点到 B 点的方向。基本上我想说:“我如何获得从101房间到143房间?” (或三倍奖励积分,从 101 房
我在 Win32 下的 MS dev studio 中将一些代码从 C 转换为 C++。在旧代码中,我使用 QueryPerformanceCounter() 进行了一些高速计时,并对获得的 __in
我正在寻找一种方法来从 QTableWidget 单元格中挤出所有可能的空间。我粘贴了一些代码来演示我在下面尝试过的事情,并且我上传了在 OSX 10.8.5、Qt 4.8.4、PySide 1.2.
对于 PhoneGap 移动应用程序,我有以下场景: 应用程序打开并显示index.html 用户登录并被重定向到 home.html 然后用户可以转到 news.html 我想以一种方式处理 hom
可能是一个简单的问题: 尝试使用plotly生成散点图并自定义图例。 这是我的数据: require(plotly) set.seed(1) my.df % add_annotations(text=
在 Instagram/Facebook 上的帖子下方,您会看到类似“第 1 个人、第 2 个人和其他 10 个人喜欢此帖子”的文字。我想知道如何使用 Django 来实现网站的相同效果?通常,我会使
我必须使用 Form.Action 重定向到将从我的页面获取值的脚本。值得注意的是,这个脚本是外部的。 我的问题是,我还想要被点击并连接了 Action 的按钮,以便首先在后面的代码中完成一些功能。
我有一个问题。 我可以在 Express 中操纵路线吗?当我发出 get 或 post 申请地址时,我可以将 2 条路线传递到同一个 get 或 post 申请吗?像这样的东西。 module.exp
如果我在不同的 Canvas 上绘制我的图像,我可以使用 js 库应用一些效果,如淡入淡出、移动和其他效果吗?如果可能的话,在速度和性能方面可能会很有趣,而不是操纵“原始”?非常感谢 最佳答案 if
我希望你能帮助我。如何转换这个扁平的 JavaScript 数组,来自: [ {"state":"First State","district":"First District","local_
我有一个 React JSX 元素,我想遍历它的子元素,对其中的每个字符串元素执行替换(或任何其他操作),然后返回新的、修改后的 JSX 元素。例如: var element = Text { var
我正在制作一个 Java 网络项目,我想在其中使用一些 javascript 操作一些前端 SVG 元素。 我正在绘制一个相当简单的车库图,其中有一个来自 JSP 页面的棚屋元素。 我想要一个 jav
我读过许多帖子,其中人们询问有关对 XML 元素强制执行某些属性顺序的问题,一般的回答是这不合法/必需/不允许/相关/其他。 我不是在寻找任何说我不应该关心属性顺序的回复,所以如果这是你的观点,请不要
大家好,我的问题是,如何将两个 C 风格的字符串 append 到一个字符串中? 我对 C++ 的处理方式 (std::string) 很感兴趣,我从未接触过 C 风格的字符串,需要为我当前的开发项目
我选择了 HTML 页面上的所有节点,如下所示: var all = $('*'); 然后我遍历每个节点,检查每个节点是否有关联的文本值: var newDom = all.map((i, node)
示例取自 Mozilla's help page re = /(\w+)\s(\w+)/; str = "John Smith"; newstr = str.replace(re, "$
我是一名优秀的程序员,十分优秀!