gpt4 book ai didi

javascript - 善与恶——操纵弦乐

转载 作者:行者123 更新时间:2023-11-30 23:58:06 27 4
gpt4 key购买 nike

描述

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:

  • 霍比特人:1
  • 男性:2
  • Sprite :3
  • 矮人:3
  • 老鹰:4
  • 巫师:10

在邪恶方面,我们有:

  • 兽人:1
  • 男性:2
  • 座狼:2
  • 哥布林:2
  • 乌鲁克海:3
  • 巨魔:5
  • 巫师:10

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' );

我的所有测试都失败了,我错过了什么?

最佳答案

关于测试失败的原因,您有两个问题:

1) 您将按 '' 进行拆分,而不是 ' ',后者基本上是在每个字符后进行拆分。

'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' ]

2) 假设您修复了问题#1,它仍然会失败。这是因为分割的结果将是字符串,并且使用 + 添加字符串将它们连接起来。

将会发生的情况的示例:

'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
<小时/>

TL;DR

要修复代码,您必须替换此...

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com