View Full Version : IF statements????
bk4_BJ
03-12-2003, 02:41 PM
I am used to the C++ way of doing IF statments
if(condition){
what you want to do;
blah blah blah;
}
I have looked almost everywhere on the Internet to find an answer. What is the format of an IF statement in Javascript. I should know how to do everything else if i could only get the layout of it. This site is for a calculator that we are doing in school, you have to display a different random number in 2 text boxes and then the user has to add them and give you the answer. If he/she is wrong then you tell them, likewise if they are right. Please help!!! I ask the teacher and she answers my question with a question:mad: THX
brothercake
03-12-2003, 03:12 PM
it's exactly the same:
if (condition) { true; }
else { false; }
or, for more conditions:
if (condition1) { statement1; }
else if (condition2) { statement2; }
else { statement3; }
For complex sets of conditions, a switch statement is useful:
switch(condition) {
case "this" :
statement1;
break;
case "that" :
statement2;
break;
default :
statement3;
break;
}
JS also has ternary expressions:
(condition)?true:false;
bk4_BJ
03-12-2003, 03:29 PM
You can use the switch() on JS?????? That is awsome, i love the switch(). Ok, so after i make my conditions and find out what i want to "cout", how do i display it?? Lets say the problem is
4 + 8 = x
being brilliant you type
12
since 12 equals the eval of 4 and 8, i need to send a message saying so. How would i do that????
num1 = eval(4)
num2 = eval(8)
var answer = form.name.value
if(num1 + num2 == answer)
/*what do i put here to show: "Good job you got it right", and where would it show up(i made a table to hold the 2 text boxes, the button, and the answer text box. So the dimensions are
2 wide*3 tall????)*/
else
//and here "Sorry try again"
bk4_BJ
03-12-2003, 05:48 PM
Can someone please respond and help me?? :(
brothercake
03-12-2003, 07:40 PM
Originally posted by bk4_BJ
Can someone please respond and help me?? :(
Remember this a public forum with members all around the world, in different time zones :)
Anyway - you don't need eval() to do arithmetic; here are some examples:
var answer = 6 + 10; // outputs 16
var six = 6;
var ten = 10;
var answer = six + ten; // outputs 16
but note that when you read values from a form, they're always strings. So if you have two boxes with the 6 and 10 values in, and you went like this
var six = document.mathform.num1.value;
var ten = document.mathform.num2.value;
then you would get a string concetenation:
var answer = six + ten; // outputs "610";
There are several ways of turning strings into numbers:
var six = parseInt(document.mathform.num1.value);
or for a float
var six = parseFloat(document.mathform.num1.value);
there's also a Number() built-in function, and a toNumber() method:
var six = document.mathform.num1.value.toNumber();
var six = Number(document.mathform.num1.value);
You can write values back to textboxes with the opposite of that read statement
document.mathform.ans.value = answer;
Roy Sinclair
03-12-2003, 08:49 PM
var six = parseInt(document.mathform.num1.value,10);
Watch out and make sure you always specify the base when using the perseInt function. Otherwise you'll get a user entering 010 as the number and wondering why they got 8 as the value.
brothercake
03-12-2003, 08:59 PM
Originally posted by Roy Sinclair
Watch out and make sure you always specify the base when using the perseInt function. Otherwise you'll get a user entering 010 as the number and wondering why they got 8 as the value.
Hey that explains a few things .... Thanks
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.