I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
Asked
Active
Viewed 168 times
-5

Joe
- 46,419
- 33
- 155
- 245

CodeMaster
- 15
- 3
-
1See also: http://stackoverflow.com/questions/4090491/first-element-in-array-jquery – 000 Mar 27 '14 at 16:44
-
I'm sorry I am new to coding and I didn't know that solution[0] was a thing, thank you. – CodeMaster Mar 27 '14 at 16:49
-
3You'll learn what's "a thing" and what's not if you take the time to read a beginners JavaScript tutorial. – cookie monster Mar 27 '14 at 16:51
-
4I like that the OP's name is CodeMaster. – Andy Mar 27 '14 at 16:52
3 Answers
0
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined
if the array was:
var solution = [cow]

Alex
- 37,502
- 51
- 204
- 332
0
Is solution an array, or is it in that form? (var solution = [cow, pig]
) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);

Ryan
- 14,392
- 8
- 62
- 102
0
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow

Doomenik
- 868
- 1
- 12
- 29