...

View Full Version : Need Help on this function ()



Mewanto
12-14-2006, 07:38 PM
Helloo all of javascript's Guru,

why i couldn't have z as x*y ?

Thank you


<script type = "text/javascript">
var x,y,z;


document.write("How much is " + getRandom(x) + " times " + getRandom(y) +"<br\>");
document.write("The answer is " + getRandom(z) );

function getRandom()
{

x = Math.floor( 1 + Math.random() * 9);
y = Math.floor( 1 + Math.random() * 9);
z = (x * y);
return x,y,z;

}

david_kw
12-14-2006, 07:52 PM
The function is fine other than it returns too many values. Take out the whole return line in the function.

Then call the function before the document.write lines like

getRandom();

Finally since getRandom() doesn't return anything anymore and since it has already been called to set the values x, y, and z, change "getRandom(x)" to just "x" and the same with y and z in the document.write lines.

That should work. This sounds like it might be homework so no I'm not going to do it for you, but I am willing to give pointers when I have the time and inclination.

Good luck with it.

david_kw

Mewanto
12-14-2006, 08:10 PM
Thank you. i forgot i can use getRandom() instead of using getRandom(x) or something.

anyway, this is not a homework. i just got bored and try to learn javascript.

Thank you again :) david. It's work now

david_kw
12-14-2006, 08:30 PM
A worthy goal. :)

Mewanto
12-15-2006, 05:09 AM
This what i am currently making.

When i have answered a correct answer and generate new question. why there are not have any inputVal ?.

Help please .. Pointer should be enough.. so i can imporve my self

Can i load "checkAnswer" again?. so i can have inputVal again.

Thank you in advance



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Function Random Number</title>
<script type = "text/javascript">
var x,y,z;


getRandom();
document.write("How much is " + x + " times " + y +"<br\>");
document.write("The answer is " + z );


function getRandom()
{
x = Math.floor( 1 + Math.random() * 9);
y = Math.floor( 1 + Math.random() * 9);
z = x* y
return x,y,z;
}

function getNewQuestion()
{
getRandom();
document.write("How much is " + x + " times " + y +"<br\>");
document.write("The answer is " + z );
}

function checkPressed()
{

var check = checkAnswer.inputVal.value;
var checkValue;

checkValue = parseInt( check );

if (checkValue != z)
window.status = "No. Please try again.";

else
{
window.status = "Very good";

}

if (checkValue == z)
getNewQuestion();
}

</script>
</head>


<body>
<form name = "checkAnswer" action = "">
<p> Enter The Answer <br/>
<input name = "inputVal" type = "text"/>
<input name = "Enter" type = "button" value = "search"
onclick = "checkPressed()" /><br /></p>

</form>
</body>

</body>
</html>

david_kw
12-15-2006, 07:51 AM
One problem I see. This

var check = checkAnswer.inputVal.value;

should be

var check = document.checkAnswer.inputVal.value;

david_kw

Mewanto
12-15-2006, 08:53 AM
Thank you david_kw.

It's still a problem - i cant get back the input textbox -.-.

i will try again. if you have any pointers, please let me know ya. Pointer should be enough, so i can learn to understand.

david_kw
12-15-2006, 09:30 AM
When you do document.write() after the inital load it will rewrite over everything in the page. It can be useful when the page is building but document.write isn't very useful once the page has been loaded.



function newWrite(n, s) {
document.getElementById("writediv" + n).innerHTML = s;
}
.
.
.
getRandom();
newWrite(1, "How much is " + x + " times " + y);
newWrite(2, "The answer is " + z );
.
.
.
<div id="writediv1"> </div>
<br />
<div id="writediv2"> </div>


This is untested but it might give you an idea how else it could be done.

david_kw

Mewanto
12-15-2006, 10:05 AM
Thank you :). i figured it out..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum