PDA

View Full Version : Parameter Used to Update Formula


swtrans
03-16-2006, 02:35 PM
If this was working correctly the alert box's value would equal 1.

Any help would be appreciated. Thanks!!

------------------------------------

<script type="text/javascript">

var answer01 = 0;
var answer02 = 0;

function show(obj)
{
answer(obj) = 1;
}

function doit(param)
{
show("+param+");
alert(answer01);
}
</script>
<input type="button" value="Answer01" onclick="doit('01')"><br><br>
<input type="button" value="Answer02" onclick="doit('02')">

Beagle
03-16-2006, 02:59 PM
var answer01 = 0;
var answer02 = 0;

function show(obj)
{
answer(obj) = 1;
}

I think you want an array:


var answers = new Array();

function show(obj)
{
answers[obj] = 1;
}

function doit(param)
{
show("+param+");
alert(answer['01']);
}

swtrans
03-16-2006, 03:13 PM
I'll try messing with an array. Is there any way to do this without an array?

Kor
03-16-2006, 04:36 PM
yes

window['answer'+obj]=1;

following the fact that variables and functions are "attached" to the window object.

swtrans
03-16-2006, 05:07 PM
Kor, am I doing this right? Thanks!

<script type="text/javascript">

var answer01 = 0;
var answer02 = 0;

function show(obj)
{
window['answer'+obj]=1;
}

function doit(param)
{
show("+param+");
alert(answer01);
}
</script>
<input type="button" value="Answer01" onclick="doit('01')"><br><br>
<input type="button" value="Answer02" onclick="doit('02')">

Kor
03-16-2006, 05:35 PM
Do you need 2 functions by all mean?

...
var answer01 = 0;
var answer02 = 0;
function doIt(q){
window['answer'+q]=1;
alert(window['answer'+q])
}
...


This way you modify dynamically the value of the variables answer01,answer02...and so on

In fact you may use the window[something] notation even to set your initial values.

var nr =15;//nr of answers
for(var i=0;i<nr;i++){
var q=((i+1)<10)?'0'+(i+1):i+1;
window['answer'+q]=0;
}
function doIt(q){
window['answer'+q]=1;
alert(window['answer'+q])
}

Kor
03-16-2006, 05:38 PM
But in fact it is not much different by using arrays. Pretty much the same.

Kor
03-16-2006, 07:55 PM
I see now that you don't actually need to set the variables. You may use the existence/nonexistence of the variables and transform that Boolean into 0 or 1.

Is this code close to your needs?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var a='Answer'
var len;
function doIt(q){
window[a+q]=1;
writeRes()
}
function writeRes(){
var t=''
for(var i=0;i<len;i++){
var p=((i+1)<10)?'0'+(i+1):i+1;
t+=a+p+' ='+Number(Boolean(window[a+p]))+'<br>';
}
t=t.replace(/<br>$/,'');
document.getElementById('res').innerHTML=t;
}
onload = function(){
var but= document.getElementById('ans').getElementsByTagName('input');
len=but.length;
for(var i=0;i<len;i++){
but[i].b=((i+1)<10)?'0'+(i+1):i+1;
but[i].onclick=function(){doIt(this.b)}
}
}
</script>
</head>
<body>
<div id="ans">
<input type="button" value="Answer01"><br><br>
<input type="button" value="Answer02"><br><br>
<input type="button" value="Answer03"><br><br>
<input type="button" value="Answer04"><br><br>
<input type="button" value="Answer05">
</div>
<div id="res">&nbsp;</div>
</body>
</html>

-----
In fact... can you go further and detail your aim? It lookks like you need to check the true/false of some choices. Maybe we could find even a shorter way to do that.