美文网首页
善恶对抗#JS_codewar_8

善恶对抗#JS_codewar_8

作者: bbjoe | 来源:发表于2017-06-01 22:44 被阅读0次

题目

两个参数,一个叫good,一个叫evil。
格式都为“整数(空格)整数(空格)整数……”的字符串,good含有6个整数,evil含有7个整数。
不同位置的整数有不同的价值。
good的是:[1, 2, 3, 3, 4, 10]; evil的是[1, 2, 2, 2, 3, 5, 10]
整数与它位置对应的价值的乘积表示这个位置的总价值
所有位置的总价值汇总表示最终的总价值
求出good和evil总价值,并对比。
Description

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:

Hobbits - 1
Men - 2
Elves - 3
Dwarves - 3
Eagles - 4
Wizards - 10
On the side of evil we have:

Orcs - 1
Men - 2
Wargs - 2
Goblins - 2
Uruk Hai - 3
Trolls - 5
Wizards - 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.

Input:

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:

Hobbits, Men, Elves, Dwarves, Eagles, Wizards.
The second parameter will contain the count of each race on the side of evil in the following order:

Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards.
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.

Output:

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){
    let good_array = good.split(" ");
    let good_force = [1, 2, 3, 3, 4, 10];
    let g = 0;
    for (i=0; i<6; i++) {
         g += good_array[i] * good_force[i];
    }
    
    let evil_array = evil.split(" ");
    let evil_force = [1, 2, 2, 2, 3, 5, 10];
    let e = 0;
    for (i=0; i<7; i++) {
         e += evil_array[i] * evil_force[i];
    }
    
    let good_win = "Battle Result: Good triumphs over Evil";
    let evil_win = "Battle Result: Evil eradicates all trace of Good";
    let peace = "Battle Result: No victor on this battle field";
    let result;
    
    g != e ? result = (g > e ? result = good_win : result = evil_win) : result = peace;
    console.log(result);
    
    return result;
}

别人的代码

function goodVsEvil(good, evil) {  
  var getWorth = function( side, worth ) {
    return side.split(' ').reduce( function(result, value, index) { 
      return result + (worth[index] * value);
    }, 0);
  }

  var result = getWorth( good, [1,2,3,3,4,10] ) - getWorth( evil, [1,2,2,2,3,5,10] );

  return result > 0 ? "Battle Result: Good triumphs over Evil" :
         result < 0 ? "Battle Result: Evil eradicates all trace of Good" :
                      "Battle Result: No victor on this battle field";
}

我的感想

题目翻译起来太绕了额……

相关文章

  • 善恶对抗#JS_codewar_8

    题目 两个参数,一个叫good,一个叫evil。格式都为“整数(空格)整数(空格)整数……”的字符串,good含有...

  • 2019.1.04阅读打卡

    72-90《人类简史》知善恶树,认知革命对智人的影响。 59-63《微习惯》大脑的工作原理,前额皮层—对抗自动行为...

  • 《问佛》

    佛说: 善恶必有因。 善恶必有果。 善恶必有心。

  • 巨龙说给龙母的话

    我只是想让你摸摸我的脸 告诉我,你爱我 我便能脱下这一身邪愤戾气 化身一条龙 此生只臣服于你 为你对抗全世界 无论善恶

  • 善恶

    抬起的手 推向善 推向恶 推向是非的漩涡 低下的头 面向善 面向恶 面向的是对错 睁开的眼 看向善 看向恶 看向黑...

  • 善恶

    最近两部热播的网播剧让吃瓜群众们多了些茶余饭后的话题。一部是已经结束的《延禧攻略》,因为没看过所以不予...

  • 善恶

    从修道而言,无所谓善恶,只有阴阳的变化。宇宙大道生生不息,阴阳更替,今天的善可能是明天的恶,你眼中的善,可能是我眼...

  • 善恶

    上位者,正己身,修心,修德,一身浩然正气,邪魔不侵,外道不损,善恶一念间,望君向善,得以长存。

  • 善恶

    我们生生世世以来,曾学过佛,也曾行善积德,但是因为没有靠佛的力量,所以轮回至今。因此就我们这个法门来说,真正的善就...

  • 善恶

    凶恶者,人心之负也。 凶恶有三,心之凶恶为大,肉体之欲为次,隐之凶恶为阴险者之最也! 是以圣人,度心恶者,斥肉欲者...

网友评论

      本文标题:善恶对抗#JS_codewar_8

      本文链接:https://www.haomeiwen.com/subject/tnhyfxtx.html