473,585 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 2298
Dormilich
8,658 Recognized Expert Moderator Expert
1) yes

2) er, I didn’t understand what you mean
Dec 17 '09 #2
bnsmith001
2 New Member
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 Recognized Expert Moderator Expert
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
5679
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
6
2369
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 minimum risk of clashing (due to function/variable name clashes). Do any of you do this already and/or are there any techniques you've come...
22
4590
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and...
18
7358
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 client-side javascript. I forgot to bookmark it, and now I can't find it. Anyone?
136
9282
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
6
5576
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 object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not...
2
3047
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 what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been...
14
2123
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 and holds everything a unit in my game is supposed to do. What are some some cures for this kind of large object or are they OK because they...
2
3313
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 recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.