0
var warriorsArray = new Array();
warriorsArray[0] = Stephen Curry;
warriorsArray[1] = Andre Iguodala;
warriorsArray[2] = Klay Thompson;
warriorsArray[3] = Andrew Bogut;
warriorsArray[4] = David Lee;

function stats(); {

}
return warriorsArray[0];
console.log warriorsArray[0];

Where do I put the return function?

I also wanted to split them and then assign a their number how do I do that inside the function (Curry #30)? Thank you!

1
  • How functions work is typically explained in JavaScript tutorials, and you will find much more comprehensive information there than you could get in an answer. A list of things that are wrong in your code: a) No string delimiters around the array values. b) ; following stats() c) return statement outside a function. d) missing parenthesis for console.log function call. Given the amount of errors in this little code, I really encourage you to read a tutorial again. Commented Feb 21, 2014 at 6:28

4 Answers 4

2

This way.

You also have some syntax errors there

function stats() {
   var warriorsArray = new Array();
   warriorsArray[0] = "Stephen Curry";
   warriorsArray[1] = "Andre Iguodala";
   warriorsArray[2] = "Klay Thompson";
   warriorsArray[3] = "Andrew Bogut";
   warriorsArray[4] = "David Lee";
   return warriorsArray;
}
Sign up to request clarification or add additional context in comments.

Comments

0
function stats() {
    var warriorsArray = ['Stephen Curry', 'Andre Iguodala', 'Klay Thompson', 'Andrew Bogut', 'David Lee'];
    return warriorsArray;
}

2 Comments

function stats(); { copy&paste?
@FelixKling Thanks, you are right. I also noticed that after I posted the answer.
0
function stats()
{
var warriorsArray = new Array(5);
warriorsArray[0] = Stephen Curry;
warriorsArray[1] = Andre Iguodala;
warriorsArray[2] = Klay Thompson;
warriorsArray[3] = Andrew Bogut;
warriorsArray[4] = David Lee;

return warriorsArray;
}

1 Comment

You don't need to specify the length of the array and your strings are unquoted.
0

Just FYI, the best way to populate the array is using .push( value ):

function stats() {
   var warriorsArray = new Array();
   warriorsArray.push("Stephen Curry");
   warriorsArray.push("Andre Iguodala");
   warriorsArray.push("Klay Thompson");
   warriorsArray.push("Andrew Bogut");
   warriorsArray.push("David Lee");
   return warriorsArray;
}
console.log(stats());

2 Comments

Why push instead of indexes? Of course, you can override by mistake some existing values, is that what you were thinking?
Overriding specific values, adding and removing values, reodering values ... these are all good reasons to use .push

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.