aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Better way of doing this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Better way of doing this?" Watch "Better way of doing this?" New topic
Author

Better way of doing this?

Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Hi

I am trying to make a flash card type game.

So Basically I will have a rows of 6 checkboxes with a image on top(expect the first checkboxes of each row they are just to select rows).

Now Once the user have made there choices the user then clicks the start button.

So my orignal version of my site did not use any javascript. Basiclly everything that the table needed was in a database(and will continue to use the db even with this new javascript version). So the database would go and all the checkboxes would be dynamically made and so would all the Image boxes. Now when the use selected their choices and hit the start button. I would use a big loop and check for that is checked. I would then grab the checkbox ID of each selected checkbox. The reason I would take the ID is because the ID contained the answer(so it would be like chk_a). Where "a" is the answer. I then would grab the image urls too while I am looping around.

I would then package these in an array. So the image and the answer would always be together. Then in the end I would just unpack the stuff and display the url in the awaiting Image box and when the user typed in what they thought the image was then I would compare the their answer to the real answer.


So what I am focusing on right now is how to pack this stuff up and not sending it to the quiz(that will be a couple questions down the road since I am working on the page where the user chooses what they want)

What I am thinking to do is have a value attribute in each checkbox(this is an asp.net checkbox) which will have the Image url (that it gets from the database). Then when the user is finished making their choices I would grab all the checked boxes value attribute and parse the url. Since the url would contain the answer. then I would put this in an array and the full URL too.

Then do stuff to it later.

So the first thing I see is I don't think javascript has an arraylist. In my C# version I used a big Arraylist as the container to hold them all. So I don't know what I would do for this case.

So is there a better way of doing this. I am using Jquery by the way so I can harness the power of it(for instance finding the checked boxes will be no problem). This is one of the reasons why I am asking I am hoping that maybe jquery has something that might help or the vanilla javascript for that matter.


Incase anyone is wondering why I am making a new version here are the reasons why.

1. My old way is slow. I was just starting out with asp.net and C# and did not want to get bogged down with learning another language on top of it. So the select row thing I was talking about that selects the entire row would have to contact the server to acheive this. I fooled people by using a update panel so it would not do a full postback but it still is slow.
2. When you made it the actual quiz every time a user made a guess I had to go back to the server and check it and then display a new on. Of course this was slow.

So basically its because it slow lol.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

So the first thing I see is I don't think javascript has an arraylist.
JavaScript has arrays. What's lacking from the arrays that makes them unusable to you?
[ June 13, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Originally posted by Bear Bibeault:
JavaScript has arrays. What's lacking from the arrays that makes them unusable to you?

[ June 13, 2008: Message edited by: Bear Bibeault ]


Well I thought you have to set the number of array cells. I going to have an unknown amount that will be needed to be put in this array.

So you think the method of using array's would be best.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You do not have to declare the length with JavaScript arrays



Eric
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Originally posted by Eric Pascarello:
You do not have to declare the length with JavaScript arrays



Eric



why do you make a myArray object and then you have a different name on the next line?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Two different ways of doing the same thing.
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Hi

I found this on a website

var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.
var goodArray= [10]; // Creates an Array with 10 as the first element.

I don't understand what they mean by starts with 10 as the first element. Do they mean it starts at index 10?

They also state


Current best-practice eschews the new keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this´┐Ż


So they say that you should not use the "new" keyword due to best-practices. If this is true then how do I limit my array down to 2? I need an array that can take any amount of of items(what this [] thing can do) however I still want to have an array that only can have 2 fields and can't add onto it.

My second array should never have more then 2 fields. If it is trying to add more then 2 fields then I rather have an expection thrown then it going on its merry way.


http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Originally posted by Michael Hubele:
I found this on a website

var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.
var goodArray= [10]; // Creates an Array with 10 as the first element.

The first example creates an array with 10 uninitialized entries. The second example creates an array with one entry, that is initialized to the value 10.

So they say that you should not use the "new" keyword due to best-practices
Hogwash. Either way is valid. I tend to use the [] notation myself and can't remember the last time I used the constructor, but that doesn't make using the Array construct a "bad practice".

If this is true then how do I limit my array down to 2?

I'm not sure what you mean by that. If you only want 2 entries, only create 2 entries. Simple as that.

I need an array that can take any amount of of items(what this [] thing can do) however
An array can take any number of entries regardless of how it is created.

I still want to have an array that only can have 2 fields and can't add onto it.
That completely contradicts your previous statement. in any case, that isn't going to happen. If you don't want it to have more than two entries, don't give it more than two.

My second array should never have more then 2 fields. If it is trying to add more then 2 fields then I rather have an expection thrown then it going on its merry way.
Nope. See above.

If you need a construct with only two entries, why use an array? Why not an Object with two properties?

What do these two values represent?
[ June 14, 2008: Message edited by: Bear Bibeault ]
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Well what I am trying to do is. I have checkboxes and images.

First I find all checkboxes that are checked(done). I then get the value of each checkbox(which is the full path url). I then am going to stick the full path url in an array cell. Then I will trim that full url to just get the answer part(the url contains the answer) and stick that into another cell in that array.

After those have been put in that array. I am going to have another array that will insert that newly created array with the 2 cells. So in the end that array will have lots of array's in it and each of those array's inside that big array will hold the answer and the flash card image url.

So later I just unpack and use.

Like I guess I am just use to C# where when you make an array you give it a size and if you go past that many cells it will throw and out of bounds exception.


If you need a construct with only two entries, why use an array? Why not an Object with two properties?


I am not sure what you mean with this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Originally posted by Michael Hubele:
After those have been put in that array. I am going to have another array that will insert that newly created array with the 2 cells.
I'm not sure where the "two" is coming from. It seems to me there'd be as many as there are clicked checkboxes. Why only 2?

Like I guess I am just use to C# where when you make an array you give it a size and if you go past that many cells it will throw and out of bounds exception.
Java will do the same, but JavaScript is not C# and not Java (despite its unfortunate name).

I am not sure what you mean with this.
f you need a construct with only two entries, why use an array? Why not an Object with two properties?
If you have two items of data to store, an array with 2 entries isn't the best choice.

For example, let's say that you want to store a first name and a last name. You could do:That's kinda lame. Rather:Much better.
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182

I'm not sure where the "two" is coming from. It seems to me there'd be as many as there are clicked checkboxes. Why only 2?


So this is how it would be. Say the user checks 5 checkboxes. I would have one array that holds all the results and another to store each individual information.

So the first checkbox has this a value. "Image/a.gif"

So now how my program is going to work is it needs the Image path and the answer in order to setup the flash card.

In the above case the answer is "a".

So I need to make sure the answer is always with the right Image. I don't want the answer for cat to be with the image of a dog. So that why I am packaging the answer and the Image path in the same array.

So if I got 5 checkboxes I would get 5 different array's that hold the Image path and answer.

But now in this is a flashcard quiz so the user can set how many times they want to do the flashcards(could be 1 could be 500).

So I need to make away to contain all these checkboxes. So when its time to switch to a new flashcard I can randomly choose a cell from this container. Pull out that Array what has the answer and Full path and setup the flashcard quiz again.

I hope that makes more sense.


That's kinda Lame. Rather:
var name = { first: 'Bear', last" 'Bibeault');


Hmm I never seen this before. What is this called?

Also how would I access this data? name.first? for the first name?

[If you have two items of data to store, an array with 2 entries isn't the best choice.]

Is it because this way not as efficient to the otherway?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Originally posted by Michael Hubele:
Hmm I never seen this before. What is this called?

An Object.

The notation is a shorthand way of doing this:

And yes, the properties (first and last) are accessed via the dot operator.

As I recall, you have a copy of jQuery in Action. Read Appendix A. It contains a primer to all the JavaScript that you need to know.

This would be a much better way to store your two values than an array where you just remember that the [0] entry means one thing and that the [1] entry means another.
[ June 14, 2008: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Better way of doing this?