-5

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.

Joe
  • 46,419
  • 33
  • 155
  • 245
CodeMaster
  • 15
  • 3

3 Answers3

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