472,988 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

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

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
3 2271
Dormilich
8,658 Expert Mod 8TB
1) yes

2) er, I didn’t understand what you mean
Dec 17 '09 #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
8,658 Expert Mod 8TB
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

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Dan Webb | last post by:
Hi All, Im currently working on ways of pacakaging javascript functions/variables/objects in a similar way to the Java package statement so that scripts can be interact with each other with the...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.