473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2287
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.