By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Pure JavaScript Question -- working with large collection of data objects...

P: 2
I have a text file with 29766 words in it.
Currently, the file is formatted with one word per line, followed by a DOS CR/LF.

I'd like to write a Word-of-the-Day tool solely in javascript, where I "roll" a random number from 1 to 29766, read down to the "word of the day".

1) Is this fairly simple to write this word-of-the-day tool in pure javascript?

Okay, harder question (maybe)...

2) Is it also fairly simple to create random lists of a enterred list count from the file mentioned above... to create a "Word-of-the-day" list for a newsletter... either a weekly or monthly newsletter?

I have been a programmer for years in other languages, but I am fairly new to JavaScript. Please, be gentle.

Thanks in advance.
Dec 17 '09 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
1) yes

2) er, I didnít understand what you mean
Dec 17 '09 #2

P: 2
I asked --
1) Is this fairly simple to write this word-of-the-day tool in pure javascript?

The answer was
1) yes

Review -- the answer was an answer, but (politely) it was not very polite or useful. So, for the "helpful" people out there, I'll restate...
1) Can you provide simple sample code or a discussion of the commands required to show me how fairly simple it is to write this word-of-the-day tool in pure javascript, since my hours of research online resulted in NO EXAMPLES of reading a file on a website and processing it?

My second question was --
2) Is it also fairly simple to create random lists of a enterred list count from the file mentioned above... to create a "Word-of-the-day" list for a newsletter... either a weekly or monthly newsletter?

The answer was --
2) er, I didnít understand what you mean.

Review -- the answer was an answer to a question that was asked as completely as I could, given that I have only been coding seriously in JavaScript for little more than a week.
My question came as an extension of the previous question.
While I was writing the second question, I was thinking as a designer wanting to extend the idea (or modify it slightly).
Again, I am still working through the solution.
Yet, let me (the person trying to ask for help) explain, a little more... having no clue about file handling in JavaScript, but having a general idea about what I'm wanting to do. Since I don't know JavaScript, I'll have to present the idea in a pseudo-code type of presentation... hoping that someone can look it over with JavaScript in mind, and help me figure out the best way to implement it... again I'll state, since I'm not that knowledgeable of Javascript development yet.

If I wanted to look at the file from the first discussion, and pull a certain number of unique words (to be defined before the function is called by a form field)...

Expand|Select|Wrap|Line Numbers
  1. Function ReadXWords (num_required)
  2. {
  3.   // I need a variable to count the words that I've read.
  4.  
  5.   For loop_val = 1 to num_required
  6.      Array [ loop_var ] = (random() * 29766) + 1
  7.  
  8.   // call a function or method to sort the values in the array 
  9.   // into ascending order
  10.  
  11.   // open the web wordfile  
  12.   // handle the exit if the wordfile does not exist 
  13.  
  14. ( only if it's required to set the end-of-file condition on empty-file before the while)
  15.   // read a line from the file 
  16.  
  17.   // if I had to read before the while loop, then use the following logic
  18.   --  loop2_var = 1
  19.   // if I did not have to read before the while loop, then use the following logic
  20.   --  loop2_var = 0
  21.  
  22.   array_index = 1
  23.  
  24.   // while (not (eof on wordfile)) and (array_index <= num_required)  
  25.  
  26.   // //   if loop2_var == Array [ array_index ]
  27.   // // //     store word read from file into found_word_array [ array_index ]
  28.   // // //     increment array_index by 1
  29.   // //       end /* if loop2_var == Array [ array_index ] */
  30.   // //    increment loop2_var by 1
  31.   // //    read a line from the file
  32.  
  33.   // end the while loop
  34.  
  35.   // *  At this point, found_word_array has the num_required elements
  36.   // *  and has pulled random words from the file.
Hopefully, I described well enough what I'm trying to do in general terms, and I can get some tips and suggestions about things to consider...
--- use a hash...
--- don't use a hash...
--- yes it is simple... look at this thread and/or this code.
--- yes it is difficult in JavaScript, but look at this thread and/or this code.
--- you might want to read about {topic} at {web address}

Thank you in advance for helping me with this strings from a file project.

Peace.
Dec 17 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
1)
  1. getting a word from a random list:
    supposing you have the words in an array, you can generat a random number ([0, 1[) with the Math.random() function (which you obviously have to multiply with the array length and round it (Math.floor()). with this number you access the array and you get a random word
    Expand|Select|Wrap|Line Numbers
    1. var words = […]; // array of words
    2. var index = Math.floor(words.length * Math.random());
    3. var word  = words[index];
  2. reading a file: this works only for files, located on the server (JavaScript has no permissions to read local files). there are basically 2 ways to handle that
    • code the list of words as a JavaScript Array and include it like any external script file (easy)
    • make an AJAX request to get the file as is (complex)
    depending on how your original list looks like, you may choose one or the other. if you choose AJAX you have to transform the content into an array, but I can’t tell anything until I know how the file looks like*.
2)
  1. repeat step 1) as many times as required. the only difference is that you don’t access the words (array[index]) but remove the words from the array to prevent double entries (I think Array.slice())
  2. JavaScript does not possess file handling capabilities. any files you want to create/modify have to be handled server side (PHP, Perl, Python, etc.)
  3. however, you need to pass the selected words to a server script, that generates the file. this may be an AJAX call, it may as well be a form (which you can create on the fly (though I’m not a form expert))

* you can of course change the file, so that it best fits the needs… (which may be changed while you write the programme)

resources:
Mozilla Developer Center – great reference site
check out this forum’s Offsite Links

Hopefully, I described well enough what I'm trying to do in general terms, and I can get some tips and suggestions about things to consider...
this was an example of a question asked the smart way.

remember that we ‘experts’ only have the information, you provide us, that is, the more information you give, the better we understand your problem, the more helpful our answer will be.
Dec 17 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.