13 - Conclusion (What's wrong with my code?)


#1

Tried to make my own code for this exercise where the code should ask how many players there are, their names and then prints the names out.
I'm not sure I've made the array properly, no error messages appear but the console doesn't log the array


var playerNumber = prompt("How many players are there?");

var x = playerNumber;

for (x = 1; x <= playerNumber; x++)
{
var player = [prompt("What is the name of Player " + "" + [x])]

for (var i = x; i < player.length; i++)
{ 
console.log(player[i]);
 }

}


#2

Why the
[ ]
around the prompt ?


#3

On the arrays previously in the lessons, they all had square brackets. So I put square brackets on it :confused:


#4

So you were trying to pass this as an array?

Remove the [] around the prompt. :slight_smile:


#5

It still doesn't print out the array through :cry:


#6

Okay if you call player as a global variable your code will run,

var playerNumber = prompt("How many players are there?");

var x = playerNumber;

var player = prompt("What is the name of Player " + "" + [x])

for (x = 1; x <= playerNumber; x++)
{

for (var i = x; i < player.length; i++)
{ 
console.log(player[i]);
 }

}

But you need to know that it will not bring out the desired output yet, I'll leave that for you to figure out. :wink:


#7

I figured it out and got it working!

Thanks for your help!!!! :smile:


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.