Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-26-2003, 04:52 AM   #1 (permalink)
jcramer83
Registered User
 
jcramer83's Avatar
 
Join Date: Mar 2003
Location: Kansas
Posts: 3
jcramer83 is on a distinguished road
Unhappy Javascript Function

Ok, this is my first time posting on this site, so bear with me.

I'm working on a project for my javascript class, and it involves creating a page that comes up with two random one digit numbers, and you put a document.writeln() that contains "What is 5 times 3 "(for example).

The user has to input a number for what they think is the answer. If the user is correct, the page is supposed to say in the status bar "Very good" and create a new problem. And if the user is incorrect, it repeats the same question over and over until the user is correct.

Now here's my problem, we are supposed to use a function, and its supposed to be called both at execution of page, and everytime the user is correct.

I have it coded to the point that when you run it, it will give you a math problem, and will display the correct msg in the statusbar. But thats it, it wont repeat. Did that w/o functions. Now I'm trying to get it to do what I want it to w/ functions, but there's something I am missing.

If you all want, I could post the code later...

Any help as to the right direction to go with this would be very helpful!
jcramer83 is offline   Reply With Quote
Old 03-26-2003, 05:06 AM   #2 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
haven't really programmed in javascript too much, nor in a while.
but have you tried a loop?
alpha is offline   Reply With Quote
Old 03-26-2003, 09:42 AM   #3 (permalink)
jcramer83
Registered User
 
jcramer83's Avatar
 
Join Date: Mar 2003
Location: Kansas
Posts: 3
jcramer83 is on a distinguished road
I've tried to use if/else statements. This is how far I got before starting to mess with defining/calling functions:


number1 = Math.floor( 1+ Math.random() * 9 );
number2 = Math.floor( 1+ Math.random() * 9 );

document.writeln("<h1>How much is " + number1 + " and " + number2 + "?</h1>" );

answer = window.prompt( "Correct answer?" );

number3 = number2 * number1


if ( number3 == answer )
window.status = "Very good!";

else {

window.status = "No. Please try again.";

}


But, it would show the "very good" if the answer was correct, and if it was wrong, it would show the "no, try again."

The instructions want me to use a function at execution and whenever the user answers correctly.

However, I am having dificulty defining/calling the functions.

Is what I have done on the right track? Or am I going to have to start over?
jcramer83 is offline   Reply With Quote
Old 03-26-2003, 10:38 AM   #4 (permalink)
jcramer83
Registered User
 
jcramer83's Avatar
 
Join Date: Mar 2003
Location: Kansas
Posts: 3
jcramer83 is on a distinguished road
Alright, after experimenting with functions, this is what I have, now when its executed, it now gives the user a new question each time they answer correctly, and puts "yes, your correct" into the status bar.

However, if the user answers incorrectly, and answers right on the 2nd or Nth try, it does give them a new question, but wont change on the status bar (still says "No, try again")

document.write( myfunc() );


function myfunc() {

number1 = Math.floor( 1+ Math.random() * 9 );
number2 = Math.floor( 1+ Math.random() * 9 );
number3 = number2 * number1;

document.writeln("<h1>How much is " + number1 + " and " + number2 + "?</h1>" );
answer = document.writeln( "Correct answer?" );

document.write( myfunc2() );

return;


}



function myfunc2() {


if ( number3 == answer ) {
window.status = "Very good!";

document.write( myfunc() );

}

else if ( number3 != answer) {

window.status = "No. Please try again."

answer = window.prompt( "Correct answer?" );
document.write( myfunc() );


}


}
jcramer83 is offline   Reply With Quote
Old 03-27-2003, 08:36 AM   #5 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
quick help

I started writing a response to this last night and then my cable modem went on the fritz and wouldn't let me connect until the morning. So now I'm here, at school, with like no time so I'll try and help you a little bit.

First suggestion, make more functions. Make your functions do one small part of the over all program. Make a whole seperate function for the document.write(stuff) calls.

Also, please name your functions to give some kind of indication of what they do. Please comment your code as well it'll make other people more able to help you.

It seems like you're really close so these few changes should make the fix very simple. So much so you may be able to do it yourself.
saline is offline   Reply With Quote
Old 04-14-2003, 06:12 AM   #6 (permalink)
mouren
Registered User
 
Join Date: Apr 2003
Location: Salt Lake City
Posts: 2
mouren is on a distinguished road
sounds like you have everything in order, but you were saying that the message won't clear out of the status bar when the user finally get is right

first check to make sure that in your myfunc2 function you assign the value of 'window.status = "";' right after they submit another answer - this way the status bar is blank before submitting a new value to it depending on their answers

hope this helps
mouren is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript function help.. Admin HTML, XML, Javascript, AJAX 2 03-09-2003 06:35 PM
pass filename to function Blaqb0x Standard C, C++ 1 09-02-2002 10:12 PM
trouble calling auxiliary function in a class brtklt Standard C, C++ 0 07-18-2002 05:22 PM


All times are GMT -8. The time now is 02:17 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting