BattleSim

So, guys, I’m trying to make a battle simulator in JS and it’s comming along great… until I got here. The console keeps saying “TypeError: compA is null”. What should I do? And the function keeps printing “undefined” in the console instead of the soldier’s names. The same is for the sentence. com1 and com2 are compA and compB.

alert(“Welcome to BattleArena!”)
var compA = prompt(“So who is the challenger?”)
var compB = prompt("…and who will be defending their title?")
alert(“LAAADDDDDDIIIIIEEEEESSSSS and GENNNNNNNNNTTTTTLLLEEEEEMMMEEEENNN…”)
alert(“BOOOOOOOYYYYYYYYSSSSSS and GGIIIIIIRRRRRLLLLSSS…”)
alert(“TODAY, we have summoned “+ compA.name +” and “+ compB.name +” to battle in the world famous… BBBBBAAAAAATTTTTLLLLLLLEEEE AAAAAARRRRREEEEENAAAAAA!”)
alert(“Who will keep their title today…”)
alert("…and who will go home a disgrace…?")
alert(“BBBRRRRAAAAAAAWWWWWWLLLLLLLERRRRRRSSSSSS…”)
alert(“Begin!”)

var battleSim = function(comA, comB){
console.log(comA.name)
console.log(comB.name)
}
battleSim(com1, com2)

You haven’t defined com1 or com2

You also haven’t given compA or compB .names

In a perfect world, one would put a semicolon at the end of each of your alert() functions.

I would change your code to be like this:

alert("Welcome to BattleArena!");
var compA = prompt("So who is the challenger?");
var compB = prompt("...and who will be defending their title?");
alert("LAAADDDDDDIIIIIEEEEESSSSS and GENNNNNNNNNTTTTTLLLEEEEEMMMEEEENNN...");
alert("BOOOOOOOYYYYYYYYSSSSSS and GGIIIIIIRRRRRLLLLSSS...");
alert("TODAY, we have summoned "+ compA +" and "+ compB +" to battle in the world famous... BBBBBAAAAAATTTTTLLLLLLLEEEE AAAAAARRRRREEEEENAAAAAA!");
alert("Who will keep their title today...");
alert("...and who will go home a disgrace...?");
alert("BBBRRRRAAAAAAAWWWWWWLLLLLLLERRRRRRSSSSSS...");
alert("Begin!");

var battleSim = function(comA, comB){
console.log(comA)
console.log(comB)
}
battleSim(compA, compB)

Does that work?

I’m quite interested to see where this is going…
:stuck_out_tongue:

It works! Thnx JJ! I just need help (if you can) making the computer print the soldiers names. That’s why I originally had .name. They were suposed to enter the name of one of the soldiers, and get their names out (1st parameter), just as a data check. Here they are:

var blu = new soldier(“Blu_Fyre”, 10, 2, 90)
var whit = new soldier(“Wite_Shado”, 8, 4, 100)
var crack = new soldier(“Kraken”, 20, 0, 55)

I’ve tried a lot of things, but I can’t figure it out, sorry! I’ll try again tomorrow when I’m not so tired, as it’s quite late here… :stuck_out_tongue:

So, guys, I’m trying to make a battle simulator in JS and it’s comming along great… until I got here. The console keeps saying “TypeError: compA is null”. What should I do?



alert("Welcome to BattleArena!")
var compA = prompt("So who is the challenger?")
var compB = prompt("...and who will be defending their title?")
alert("LAAADDDDDDIIIIIEEEEESSSSS and GENNNNNNNNNTTTTTLLLEEEEEMMMEEEENNN...")
alert("BOOOOOOOYYYYYYYYSSSSSS and GGIIIIIIRRRRRLLLLSSS...")
alert("TODAY, we have summoned "+ compA.name +" and "+ compB.name +" to battle in the world famous... BBBBBAAAAAATTTTTLLLLLLLEEEE AAAAAARRRRREEEEENAAAAAA!")
alert("Who will keep their title today...")
alert("...and who will go home a disgrace...?")
alert("BBBRRRRAAAAAAAWWWWWWLLLLLLLERRRRRRSSSSSS...")
alert("Begin!")

var battleSim = function(comA, comB){
    console.log(comA.name)
    console.log(comB.name)
}
 battleSim(com1, com2)   


This is a great idea! I’m not the best with programming, but I’m not sure if you need “.name” . I think it should work without it.

1 Like

Thnx, kitty! That makes sense…

…but how do I display the soldiers name in the function? I keeps returning as undefined (I changed the com1 and com2 to compA and compB).

In line 6, you’re calling for variables defined by the player using syntax for objects. Remove .name where you call for compA & compB here and further along in your code.