TAFE STUDENT - what other ways can I write this code?

goldy30

New Member
Messages
60
Reaction score
0
Points
0
The exercise I done for Javascript was pretty simple but I'm not sure that I've achieved the result the way I was suppose to. The question was just create an application where it gets two numbers and displays the larger number... I done this but I'm wondering what other ways is there to achieve this??

My code
HTML:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>

<script type="text/javascript">

function calc()
{
var n1 = (document.form1.n1.value);
var n2 = (document.form1.n2.value);

if    (n1 > n2)
{
document.write (n1);
}
else
{
document.write (n2);
}
}

</script>

</HEAD>

<BODY bgcolor="ededed">
<center>

<h1>Largest Number</h1>

<form name="form1">
&nbsp;
1st <input name="n1" size="3">
&nbsp;
2nd <input name="n2" size="3">
&nbsp;
<input type="button" value="Show" onClick="calc()"><p>

</form>
</center>
</BODY>
</HTML>
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
The joy of programming is that as long as it works, you've done it right ;)

From an engineers perspective, can you remove anything and still maintaing functionality?
 

goldy30

New Member
Messages
60
Reaction score
0
Points
0
The joy of programming is that as long as it works, you've done it right ;)

From an engineers perspective, can you remove anything and still maintaing functionality?

Fair enough.
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
there's also a version for a pure javascript.
HTML:
<html>
  <body>
    <script>
      x = prompt("enter a number ");
      y = prompt("Enter another number");
      z=(x>y?x:y);
     alert("the larger number is " + z+ "?");
    </script>
  </body>
</html>

the thing here is it runs one time only. there is a way to repeat the stuff so you can play with the code if you want.
 
Last edited:

goldy30

New Member
Messages
60
Reaction score
0
Points
0
there's also a version for a pure javascript.
HTML:
<html>
  <body>
    <script>
      x = prompt("enter a number ");
      y = prompt("Enter another number");
      z=(x>y?x:y);
     alert("the larger number is " + z+ "?");
    </script>
  </body>
</html>
the thing here is it runs one time only. there is a way to repeat the stuff so you can play with the code if you want.

that's cool and all but how do I use that in a form or what such?
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
You don't*. As your task was simply to "get two numbers and display the larger number" there's no need for forms.

This is a wonderful example of how important project / job specifications are. You're never asked to store or present the two numbers after the initial display so you don't. Would certainly make a fun debate with your lecturer (if they're there to do more than walk you through the curriculum, that is :p)


*I'm sure you can, but that's not the point I'm making here.
 
Last edited:

goldy300

New Member
Messages
33
Reaction score
0
Points
0
You don't*. As your task was simply to "get two numbers and display the larger number" there's no need for forms.

This is a wonderful example of how important project / job specifications are. You're never asked to store or present the two numbers after the initial display so you don't. Would certainly make a fun debate with your lecturer (if they're there to do more than walk you through the curriculum, that is :p)


*I'm sure you can, but that's not the point I'm making here.

No worries then... thanks for the variation.
 
Top