-3

I am new to JavaScript.

for learning purpose, I am trying to create a simple word typing game.

I am just stuck on the first step that is to make an array.

So How do I convert a wordlist into an array?

var words = [greet
campaign
coffee
care
revise
ridge
pilot
full
prison
wrestle
account
dictionary
start
giant
fast
monarch
patrol
bear
motif
detective
trouser
title
act
speaker
pursuit
penny
appear
ballet
agreement
welcome
similar
sensation
strong
fog
limited
implicit
nursery
neighborhood
infinite
refund
double
achievement
ample
socialist
intensify
nomination
visit
drag
retiree
mislead
situation
credit
oil
clarify
conscience
war
sow
suitcase
fixture
worth
fuss
remain
moment
frighten
spider
breathe
install
interactive
allow
deadly
cabin
restless
service
rise
integrity
artist
short circuit
perception
listen
arrangement
patience
creep
landscape
stain
citizen
microphone
nose
bullet
view
category
reign
horizon
news
social
reliable
passion
stroll
constitutional
adult
city];  
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
RedBrick Poem
  • 1
  • 1
  • 1
  • 3
  • 3
    That's invalid syntax... did you mean to enclose the words in quotes? You also need commas after every array item. – CertainPerformance Aug 25 '18 at 09:28
  • 1
    There are handy docs to demonstrate the syntax of the language: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – David Aug 25 '18 at 09:29
  • [check this link I think both are same question.](https://stackoverflow.com/questions/25076300/convert-word-list-into-array) – aditya kumar Aug 25 '18 at 09:33
  • yes I checked it before but coudn't understand the split function...people have answered on this thread and its much easier to understand. Thank you for your reply – RedBrick Poem Aug 25 '18 at 09:51

3 Answers3

2

Take the array content as string like this in a variable:

var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city";

and then, write

var wordsArray = words.split(' ');
console.log(wordsArray);

That's magic of JS

Ajay Singh
  • 159
  • 1
  • 1
  • 12
  • I got multiple answers all of them seems to be working perfectly..Thank you everyone for taking out the time to help me. – RedBrick Poem Aug 25 '18 at 09:53
  • If You have just started with javascript I would suggest you to go through https://developer.mozilla.org/bm/docs/Web/JavaScript. – Ajay Singh Aug 25 '18 at 10:03
1

First thing I want to say is that your syntax is incorrect. If you are trying to express a string, please use the format - var a = "fox hen lol haha";

If you meant your variable words to be a string, and each word to be separated by a space, I have the solution to your problem.

var words = "bla bla bla your words here";
var arrayOfWords = []; // Create the array to store words
arrayOfWords = words.split(" ");

And it's done. The array arrayOfWords contains all the words from the string as individual array objects.

If you are still confused, please comment below.

Sayan J. Das
  • 852
  • 10
  • 19
0

Welcome to Stack Overflow.

Here is a way to do what you want:

var words = "greet campaign coffee care revise ridge pilot full prison wrestle account dictionary start giant fast monarch patrol bear motif detective trouser title act speaker pursuit penny appear ballet agreement welcome similar sensation strong fog limited implicit nursery neighborhood infinite refund double achievement ample socialist intensify nomination visit drag retiree mislead situation credit oil clarify conscience war sow suitcase fixture worth fuss remain moment frighten spider breathe install interactive allow deadly cabin restless service rise integrity artist short circuit perception listen arrangement patience creep landscape stain citizen microphone nose bullet view category reign horizon news social reliable passion stroll constitutional adult city".split(/\s+/)

This saves you the editing headache of having to put quotes and commas everywhere

Mikkel
  • 7,693
  • 3
  • 17
  • 31