In this section, we will revisit the code initially published in part 1. We have done a fair amount of refactoring, and we have written some tests. Now we have to rework the character object to use the items and weapons. Hopefully, you will see that the code is easier to read and less error-prone.

We were able to fix up a number of the underlying issues and ensure that we have covered many edge cases. More important, though, we get to revisit the fight logic and add a magical weapon.

Let us review the code for a character and how it has evolved.

// character.js
const weapons = require("./weapons");
const bag = require("./bag");

const character = function(who, attrib) {
    attrib = attrib || {};
    const _name = who;
    const dv = (value,def ) => (typeof value === "undefined")?def:value;
    let _hp = dv( attrib.hp, 10 );
    let _level = dv( attrib.level, 1 );
    const _bag = bag("Bag of Holding",5);
    let _weapon =  dv( attrib.weapon, weapons.hand);

#programming #javascript #coding

Functional JavaScript: Writing a simple Comand-line Game (Part 4)
1.45 GEEK