English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am making a form code where you can create questions and answers in separate arrays. They will then be written on the screen through a for loop, but I need to assign each different question, different names for the answers. I can't explain it properly, but if you can help, please do. Right now, the code only allows one radio button to be clicked throughout the whole code.

The code:

http://moregfx.com/scr/test12.htm

2007-11-23 23:41:34 · 3 answers · asked by ViralRiver 2 in Computers & Internet Programming & Design

I need to know how to change my code so it works in for loops.

2007-11-24 00:14:32 · update #1

New Link:

file:///C:/Users/Riverofrhyme/Documents/Coding/test12.js

2007-11-24 00:15:49 · update #2

New Link:

http://moregfx.com/scr/test12.js

2007-11-24 00:16:20 · update #3

3 answers

For one thing, javascript arrays are zero-based so this should be id=questions[i][questions[i].length - 1] if you want the last element in the array.

Another thing, is that a legal way to initialize an array? It doesn't seem to be, I'd use:

questions[0]= new Array("question","answer1","answer2","answer3","realanswer","name1");

Also, in your document.write statement, you seem to forget you are building a string that contains html.

This:

"   "

resolves to a literal string that contains the names of your variables, not their contents. Those variables won't mean a thing to your HTML document, you need to concatenate their values instead.

And lastly, script in an external file, referenced by a script tag via src='externalfile.js' cannot contain script tags also, so remove the script markup tags in test12.js.

Good Luck!

btw, if you want to see the HTML as written by document.write, check out a little document analyzer I wrote, http://www.pagespy.com

2007-11-24 01:42:10 · answer #1 · answered by Random Malefactor 5 · 0 0

I do not know why to compute the arithmetic mean that you would want to define endless variables. Much easier to add each input to a row or col of a matrix. However to answer your question of how to assign variables in a while loop, you need to use the genvarname function and the eval function to assign a value to that variable. Here is your code edited to answer your question. a = true; n=1; while a x=input('enter a number (0 or negative to end):'); if(x>0) eval([genvarname(['A', num2str(n)]), '=', num2str(x)]) else(x<=0); disp('end'); break end n=n+1; end A much better approach however would be to use a vector/matrix to hold the inputs. You can sum an entire vector/matrix of one dimension by calling: sum(yourVectorName); You can also get the length of your vector/matrix of one dimension by calling: length(yourVectorName); And if by any chance you wanted to know what was the 5th number entered, call: yourVectorName(5,1); or yourVectorName(1,5); depending if they are columns or rows. Here is better code. a = true; inputs = []; while a x=input('enter a number (0 or negative to end):'); if(x>0) %Matlab will complain that it is resizing every loop %just ignore since you are terminating anyway inputs(length(inputs)+1,1) = x; else(x<=0); disp('end'); break end end

2016-04-05 06:10:43 · answer #2 · answered by Anonymous · 0 0

create a form
put input commands with each its name

to see examples look at the code at
www.pekiyi.150m.com
test pages

2007-11-23 23:48:37 · answer #3 · answered by iyiogrenci 6 · 0 0

fedest.com, questions and answers