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)
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)
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)
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)
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.