Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

brandonlind2
brandonlind2
7,823 Points

Does anyone know why the player object isn't working?

The player object isn't working and its damage method isn't I'm not sure if there is a cause and effect relationship with that or if they are separate issues. the player object is the last thing, the rest is just the functions and objects it works with. I would appreciate it if if anyone can figure this out, thanks

//ammo
function Ammo(damage,name){
    this.damage=damage;
    this.name=name;
} 
var ammo={
    f556: new Ammo(80,'5.56'),
    t308: new Ammo(300,'.308'),
    s76239: new Ammo(150,'7.62x39'),    
};

//weapons and armor functions
function MeleeWeapon(damage,speed,range,energy,name){
    this.damage=damage;
    this.speed=speed;
    this.range=range;
    this.energy=energy;
    this.id=0;
}
function Guns(accuracy,damage,speed,capacity,ammo,range,name){
    this.accuracy=accuracy;
    this.damage=damage;
    this.speed=speed;
    this.capacity=capacity;
    this.ammo=ammo;
    this.range=range;
    this.name=name;
}
function RangedWeapon(accuracy,damage,speed,capacity,ammo,range,name){
    Guns.call(this,accuracy,damage,speed,capacity,ammo,range,name);
    this.id=0;
    this.attachments= []; 
        this.upgrades= [];
}

RangedWeapon.prototype= Object.create(Guns.prototype);

RangedWeapon.prototype.attachmentAdd= function(attachment){ 
        this.attachments.push(attachment);}

RangedWeapon.prototype.attachmentRemove=function(attachment){
        var index= this.attachments.indexOf(attachment);
        this.attachments.splice(index,1);
        }

RangedWeapon.prototype.upgradeAdd= function(upgrade){
    this.upgrades.push(upgrade);}

function Attachment(accuracy,damage,speed,capacity,ammo,range,name){
    Guns.call(this,accuracy,damage,speed,capacity,ammo,range,name);
}

Attachment.prototype= Object.create(Guns.prototype);


var meleeWeapons= { 
    baseBallBat: new MeleeWeapon(15,30,5,15,'BaseBall Bat'),
    kabarKnife: new MeleeWeapon(80,80,2,2,'Kabar Knife'),
    kitchenKife: new MeleeWeapon(80,75,2,3,'Kitchen Knife'),
    bowieKnife: new MeleeWeapon(80,70,2,3,'Bowie Knife'),
    };




var rangedWeapons= {
    mdr: new RangedWeapon(90,ammo.t308.damage,80,20,ammo.t308,400,'mdr'),
    tavor: new RangedWeapon(70,ammo.f556.damage,90,ammo.f556,200,'tavor'),
    ar15: new RangedWeapon(85,ammo.f556.damage,80,30,ammo.f556,300,'AR15'),
    ar10: new RangedWeapon(85,ammo.t308.damage,65,20,ammo.t308,400,'AR10'),
        akm: new RangedWeapon(60,ammo.s76239.damage,70,30,ammo.s76239,300,'AKM'),

};

var player={
    name: ' ',
    level: 1,
    health: 100,
    hunger: 1000,
    hydration: 350,
    energy: 100,
    attributes: attributes,
    skills: skills,
    inventory: [],
    equipped:{ 
         weapon: '',
         armor: { head:'',
              body:'',
                  arms:'',
              legs:''
            }
        },
    armor: 0,
    money: 0,
    damage: function(){ var equipped=this.equipped.weapon.name.toLowerCase();
            if(Object.keys(rangedWeapons).includes(equipped){       
                this.equiped.weapon.damage+=10;
            }
            else if(Object.keys(meleeWeapons).includes(equipped){
                   this.equiped.weapon.damage += 5;}
            },
    invetoryAdd: function(add){
    this.inventory.push(add);
    },
        inventoryDrop: function(drop){
    var dropIndex= this.inventory.indexOf(drop);
    this.inventory.splice(dropIndex,1);
    }
};
Konrad Pilch
Konrad Pilch
2,435 Points

Hi,

I don't know, but im learning JS, and i really like this code. Woudn't you mind if i coudl see the whole source one your finished, or parts by parts so i can learn pelase?

-Konrad

2 Answers

0yzh 󠀠
0yzh 󠀠
17,276 Points

Hey Brandon,

if you take a look at the damage method, the two conditions for your 'if' statements are missing a closing parenthesis.

damage: function(){ 
            var equipped=this.equipped.weapon.name.toLowerCase();
            if(Object.keys(rangedWeapons).includes(equipped) { // <-- closing ')' missing       
                this.equiped.weapon.damage+=10; // typo equiped -> equipped
            }
            else if(Object.keys(meleeWeapons).includes(equipped) { // <-- closing ')' missing
                   this.equiped.weapon.damage += 5; // typo equiped -> equipped
            } 
        }

There were other typing errors(invetoryAdd --> inventoryAdd), but I think that was your biggest issue. Also since attributes and skills aren't defined in the code u posted here(maybe your full .js file has it defined), I just added them to the global space and it seems to be working fine. Here is a jsfiddle example: https://jsfiddle.net/xn4fed7z/4/

brandonlind2
brandonlind2
7,823 Points

thanks :) In all honesty its kinda up in the air if I'm going to finish it, because its a practice project. I'm trying to make a text based game similar to http://www.kingdomofloathing.com but zombie survival and in javascript instead. If I finish it, I'll be sure to post it. Ill try to send it to you but I dont think you can send private messages on treehouse, If not I can just post it in the comments here if I finish it, this it what I have is far if you want to look at it. I'ts largely unfinished right now

//ammo
function Ammo(damage,name){
    this.damage=damage;
    this.name=name;
} 
var ammo={
    f556: new Ammo(80,'5.56'),
    t308: new Ammo(300,'.308'),
    n9mm: new Ammo(80,'9mm'),
    f45: new Ammo(120,'45acp'),
    s76239: new Ammo(150,'7.62x39'),
    t22: new Ammo(15,'.22'),
    t300win: new Ammo(600,'300win'),
    t3006: new Ammo(400, '30-06'),
    s76254: new Ammo(400, '7.62x54')
};

//weapons and armor functions
function MeleeWeapon(damage,speed,range,energy,name){
    this.damage=damage;
    this.speed=speed;
    this.range=range;
    this.energy=energy;
    this.id=0;
}
function Guns(accuracy,damage,speed,capacity,ammo,range,name){
    this.accuracy=accuracy;
    this.damage=damage;
    this.speed=speed;
    this.capacity=capacity;
    this.ammo=ammo;
    this.range=range;
    this.name=name;
}
function RangedWeapon(accuracy,damage,speed,capacity,ammo,range,name){
    Guns.call(this,accuracy,damage,speed,capacity,ammo,range,name);
    this.id=0;
    this.attachments= []; 
        this.upgrades= [];
}

RangedWeapon.prototype= Object.create(Guns.prototype);

RangedWeapon.prototype.attachmentAdd= function(attachment){ 
        this.attachments.push(attachment);}

RangedWeapon.prototype.attachmentRemove=function(attachment){
        var index= this.attachments.indexOf(attachment);
        this.attachments.splice(index,1);
        }

RangedWeapon.prototype.upgradeAdd= function(upgrade){
    this.upgrades.push(upgrade);}

function Attachment(accuracy,damage,speed,capacity,ammo,range,name){
    Guns.call(this,accuracy,damage,speed,capacity,ammo,range,name);
}

Attachment.prototype= Object.create(Guns.prototype);

function Armor(armor,speed,id){
    this.armor= armor;
    this.speed= speed;
    this.id=id;
}
//weapons and armor
var meleeWeapons= { 
    baseBallBat: new MeleeWeapon(15,30,5,15,'BaseBall Bat'),
    kabarKnife: new MeleeWeapon(80,80,2,2,'Kabar Knife'),
    kitchenKife: new MeleeWeapon(80,75,2,3,'Kitchen Knife'),
    bowieKnife: new MeleeWeapon(80,70,2,3,'Bowie Knife'),
    machete: new MeleeWeapon(80,40,4,1,8,'Machete'),
    baton: new MeleeWeapon(20,50,4,5,'Baton'),
    kukri: new MeleeWeapon(100,40,2,8,'Kurkri'),
    hatchet: new MeleeWeapon(80,20,3,13,'Hatchet'),
    dagger: new MeleeWeapon(80,80,2,3,'Dagger'),
    hammer: new MeleeWeapon(70,15,3,13,'Hammer'),
    golfClub: new MeleeWeapon(25,40,6,15,'Golf Club'),
    karambit: new MeleeWeapon(100,100,1,1,'Karambit'),
    huntingKnife: new MeleeWeapon(70,100,1,1,'Hunting Knife'),
    saber: new MeleeWeapon(80,40,6,10,'Saber'),
    katana: new MeleeWeapon(100,40,6,5,'Katana'),
    gladius: new MeleeWeapon(90,60,4,5,'Gladius'),
    ginunting: new MeleeWeapon(90,60,4,5,'Ginunting'),
    krisSword: new MeleeWeapon(100,50,4,5,'Kriss Sword'),
    norseSword: new MeleeWeapon(80,30,6,10,'Norse Sword'),
    armingSword: new MeleeWeapon(80,30,6,10,'Arming Sword'),
    sansibar: new MeleeWeapon(80,50,4,5, 'Sansibar'),
    krisDagger: new MeleeWeapon(100,80,2,3,'Kriss Dagger'),
    shovel: new MeleeWeapon(15,20,6,10,20,'Shovel'),    
    sludgeHammer: new MeleeWeapon(90,10,6,30,'SludgeHammer')
    };




var rangedWeapons= {
    mdr: new RangedWeapon(90,ammo.t308.damage,80,20,ammo.t308,400,'mdr'),
    tavor: new RangedWeapon(70,ammo.f556.damage,90,ammo.f556,200,'tavor'),
    ar15: new RangedWeapon(85,ammo.f556.damage,80,30,ammo.f556,300,'AR15'),
    ar10: new RangedWeapon(85,ammo.t308.damage,65,20,ammo.t308,400,'AR10'),
        akm: new RangedWeapon(60,ammo.s76239.damage,70,30,ammo.s76239,300,'AKM'),
    rem700: new RangedWeapon(100,ammo.t308.damage,30,5,ammo.t308,400,'Remington 700'),
    mauser: new RangedWeapon(100,ammo.t308.damage,30,5,ammo.t308,400,'Mauser action rifle'),
    mp5: new RangedWeapon(70,ammo.n9mm.damage,80,30,ammo.n9mm,100,'MP5'),
    ump: new RangedWeapon(70,ammo.f45.damage,90,30,ammo.f45,100,'UMP'),
    n1911: new RangedWeapon(70,ammo.f45.damage,60,7,ammo.f45,75,'1911 pistol'),
    cz75: new RangedWeapon(80,ammo.n9mm.damage,60,16,ammo.n9mm,75,'CZ 75 pistol'),
    sig226: new RangedWeapon(65,ammo.n9mm.damage,60,16,ammo.n9mm,75,'sig 226'),
    glock17: new RangedWeapon(60,ammo.n9mm.damage,60,18,ammo.n9mm,75,'Glock 17'),
    rem700Mag: new RangedWeapon(100,ammo.t300win.damage,30,5,ammo.t300win,600,'Remington 700 Magnum'),
    m1Garand: new RangedWeapon(100,ammo.t3006.damage,50,8,ammo.t3006,500,'M1 Garand'),  
    sks: new RangedWeapon(60,ammo.s76239.damage,65,10,ammo.s76239,300,'SKS rifle'),
    mosinNagant: new RangedWeapon(100,ammo.s76254.damage,20,10,ammo.s76254,300,'Mosin Nagant'),
    1022: new RangedWeapon(90,ammo.t22.damage,10,ammo.t22,100,'10/22 rifle'),
    mkiii: new RangedWeapon(90,ammo.t22.damage,10,ammo.t22,50,'MKIII pistol')
};

var attachments={
        n9mmSuppressor: new Attachment(2,0,-10,0,ammo.n9mm,0,'9mm suppressor'),
        f556Suppressor: new Attachment(2,0,-10,0,ammo.f556,0,'556 suppressor'),
        s762Suppressor: new Attachment(2,0,-10,0,[ammo.t308,ammo.s76239,ammo.s76254],0,'7.62 suppressor'),
        t22Suppressor: new Attachment(2,0,-10,0,ammo.t22,0,'22 suppressor'),
        f45Suppressor: new Attachment(2,0,-10,0,ammo.f45,0,'45 suppressor'),
        t2xScope: new Attachment(0,0,-5,0,'none',200,'2 powered scope'),    
        f4xScope: new Attachment(0,0-10,0,'none',400,'4 powered scope'),
        s6xScope: new Attachment(0,0,-20,0,'none',600,'6 powered scope'),
        t12xScope: new Attachment(0,0,-40,0,'none',800,'12 powered scope'),
        s612XScope: new Attachment(0,0,-30,0,'none',800,'6-12 powered scope'),
        t24xScope: new Attachment(0,0,-8,0,'none',400, '2-4 powered scope'),
            haloGraphic: new Attachment(0,0,20,0,'none',0,'HaloGraphic sight'),
        redDot: new Attachment(0,0,10,0,'none',0,'Red dot sight'),
        floatedArHandGaurd: new Attachment(10,0,0,0,'none',0,'free floated AR handguard'),
        t30GlockMag: new Attachment(0,0,0,12,'none',0,'30 round glock mag'),
        extended308Mag: new Attachment(0,0,0,10,'none',0,'30 round 308 mag'),
        extended556Mag: new Attachment(0,0,0,20,'none',0,'50 round 556 mag'),
        extended1911Mag: new Attachment(0,0,0,5,'none',0,'12 round 1911 mag'),
        extendedCZMag: new Attachment(0,0,0,20,'none','20 round cz mag'),
        extendedSigMag: new Attachment(0,0,0,20,'none','20 round sig mag')
};       

var armor={
       scoccerShinGaurd: new Armor(5,-1,'Soccer Shin Gaurd'),
       footBallArmor: new Armor(15,-10,'FootBall Armor'),
       footBallLeggins: new Armor(10, 5,'FootBall Leggings'),
       gasMask: new Armor(3,-10,'Gas Mask'),
       fullFacedGasMask: new Armor(15,-10,'Full Faced Gas Mask'),
       dirtBikeJacket: new Armor(15,-5,'Dirt Bike Jacket'),
       dirtBikeDeflector: new Armor(40,-25,'dirt Bike Deflector'),
       dirtBikeKnee: new Armor(15,-15,'Dirt Bike Knee Guards'),
       chainMailSharkSuit: new Armor(60,-20,'Chain Mail Shark Suit'),
       riotArmor: new Armor(80,-40,'Riot Armor'),
       militaryBallisticPlate: new Armor(30, -15,'Military Ballistic Plate'),
       };       



//generation

function weaponGen(){
    function rangedGen(){

            var number=0;
             for( var prop in rangedWeapons){
            number++    
             rangedWeapons[prop]['id']=number; 
        }
        var randomNumber= Math.floor(Math.random() * Object.keys(rangedWeapons).length ) + 1;
        var objArray=Object.keys(rangedWeapons);
        var weapon=objArray[randomNumber];
        return console.log(rangedWeapons[weapon]);
    }
        function meleeGen(){

            var number=0;
             for( var prop in meleeWeapons){
            number++    
             meleeWeapons[prop]['id']=number;   
        }
        var randomNumber= Math.floor(Math.random() * Object.keys(meleeWeapons).length ) + 1;
        var objArray=Object.keys(meleeWeapons);
        var weapon=objArray[randomNumber];
        return console.log(meleeWeapons[weapon]);
    }
    var num= Math.floor(Math.random() *2)+1;
    if(num===2){
    rangedGen()
    }
    else{
    meleeGen()
    }

}

function enviroGen(){

}

//stats

var attributes={
    perception: 0,
    intelligence: 0,
    strength: 0,
    endurance: 0,
    agility: 0,
    luck: 0
};


var skills= {
    sneak: 0,
    melee: 0,
    guns: 0,
    lockPicking: 0,
    firstAid: 0,
    cooking: 0,
    science: 0, 
};




//charaters
function Character(name,level,health){
    this.name=name;
    this.level=level;
    this.health=health;
}

var player={
    name: ' ',
    level: 1,
    health: 100,
    hunger: 1000,
    hydration: 350,
    energy: 100,
    attributes: attributes,
    skills: skills,
    inventory: [],
    equipped:{ 
         weapon: '',
         armor: { head:'',
              body:'',
                  arms:'',
              legs:''
            }
        },
    armor: 0,
    money: 0,
    damage: function(){ var equipped=this.equipped.weapon.name.toLowerCase();
            if(Object.keys(rangedWeapons).includes(equipped){       
                this.equiped.weapon.damage+=10;
            }
            else if(Object.keys(meleeWeapons).includes(equipped){
                   this.equiped.weapon.damage += 5;}
            },
    invetoryAdd: function(add){
    this.inventory.push(add);
    },
        inventoryDrop: function(drop){
    var dropIndex= this.inventory.indexOf(drop);
    this.inventory.splice(dropIndex,1);
    }
};

function leveling(){
    var max=player.level + 5;
    var min=player.level - 5;
    if(min <= 0){
         min= 1;
        }
    var level= Math.floor(Math.random() * (max - min)) + 1 + min;
    return level;
    }

var infected={
    newlyInfected: new Character('Newly Infected',1,100),
    vz1Mutant: new Character('VZ1 Mutant',leveling(),100),
    vz1soldier: new Character('VZ1 Soldier',1,100),
    vz1cop: new Character('VZ1 cop',1,100),
    infected: new Character('infected',1,100),
    vz1hazmat: new Character('Hazmat VZ1',1,100),
    vz1swat: new Character('VZ1 SWAT',1,100),
    vz1sf: new Character('VZ1 Special Forces',1,100),
    vz1gangster: new Character('VZ1 Gangster',1,100),
    vz1gorilla: new Character('Infected Gorilla',leveling(),500)
};



};