Login | Register | FAQ
Anonymous

First small step !

+ Post a reply

7 posts Page 1 of 1


First small step !

by merlin » Thu May 17, 2012 11:59 pm

Hi Guys

This is kind of like my first drawing in kindergarten. Mum would pat me on the head and proudly stick it to the fridge until I realised just how bad it was and beg her to take it down.
Similarly, I know this – my first attempt at putting some JavaScript together, is messy and full of flaws but if anyone would like to point the error of my ways and help me a little further up the path I would be more than grateful.


<!DOCTYPE html>
<html lang="en">
<head>
<title> FINDING THE LCD</title>
<meta charset="utf-8">
<link rel="stylesheet" href="#.css">
<script src="#.js"></script>
<style>
body {background:#ffdab9;font-size:2.5em}
</style>
</head>

<body>
<!-- I know this form is naively constructed but I think it shows what I'm trying to achieve -->
<form name="lcd">
Enter the first number:
<input type="text" name="lcd1" value="">
<br />
Enter the second number:
<input type="text" name="lcd2" value="">
<br />
Enter the third number:
<input type="text" name="lcd3" value="">
<br />
<input type="button" value="Calculate" onClick="findLDC";>
</form>

<script>
/* x, y and z are the numbers we want to find the lcd of and - MOST IMPORTANTLY - show the process of the calculation*/
var x = 21
var y = 15
var z = 12
var a = 2
var b = 3
var c = 5
var d = 7
var e = 11 //Not used yet
var f = 13 // ditto
var g = 17 // ditto
var h = 19 /* anything past here is probably not useful in the real world and, anyway, the main idea is to show the process of obtaining the lcd */
var m1=1
var m2=1
var m3=1
var m4=1
var m5=1
var m6=1
var m7=1
var m8=1
var m9=1
var ans=1
/* I'm guessing that I have to make the code from here a function called findLDC but - as yet - I'm not getting the form and the function to marry */

while (a>1)
{
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |_____" +x +"_____" +y +"_____" +z +"<br />")
m1=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m2=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m3=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m4=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans+"<br />" )
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m5=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}
//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m6=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m7=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m8=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

}

</script>
</body>
</html>
User avatar

merlin

  • Posts: 44
  • Joined: Sun Dec 19, 2010 3:31 pm

Re: First small step !

by XainPro » Fri May 18, 2012 7:08 am

merlin great work so far mama will be proud on you !
could you please further explain what you have tried to build in this code i only saw a form with 3 input fields that's it.
User avatar

XainPro

  • Posts: 3669
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: First small step !

by merlin » Fri May 18, 2012 11:02 am

Hi XainPro
Thanks for your kind words. Praise is always better than a “clip round the ear”

I have put a temporary link on my website which should (if it renders in your browser as it does in mine ) show how the declared variables x,y and z ( 21 , 15 and 12 )go through the stages of finding the LCD.
What I would like to do is for the student to be able to enter the values for x,y and z themselves.

As CubeSquare has pointed out to me there is a typo just before the < form >.It should be findLCD not findLDC. That seems to be an attribute that has followed me from kindergarten – sloppiness. Not good in a coder !! OH well ! I still enjoy playing in my back-yard sandpit.

http://www.merlin66.webspace.virginmedia.com/

The link is next to the ELEMENTARY MATHS box.
User avatar

merlin

  • Posts: 44
  • Joined: Sun Dec 19, 2010 3:31 pm

Re: First small step !

by XainPro » Sat May 19, 2012 6:34 am

ok merlin Good work its a very informative and good learning website for kids
is all the questions and games developed by you solely ?
User avatar

XainPro

  • Posts: 3669
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: First small step !

by merlin » Sat May 19, 2012 11:22 am

All the questions and the format are mine but a large slice of the coding development must be shared with CubeSquare. Just over a year ago I decided to learn HTML and the site was my “project”. CubeSquare came to my rescue on more than one occasion when I ran into a brick wall or was going down a blind ally.

A few months back I decided the project had achieved it’s purpose and given me a reasonable grounding in HTML so I decided to embark on learning HTML 5 and JaveScript. I’ve covered some of the basics in HTML5 but it’s JavaScript that I’ve really been trying to get to grips with. Again, I’ve made the process project based hence the LCD code I submitted. I got the script code to work but I’m struggling to link that with the form code which would let the user input their own numbers and, therefore, set the variables x , y and z for themselves.

The back-yard reference was a bit of an “in-joke” with CubeSquare.

Oh well! Back to trying to fix the marriage between my <form> and my JavaScript.
User avatar

merlin

  • Posts: 44
  • Joined: Sun Dec 19, 2010 3:31 pm

Re: First small step !

by merlin » Sat May 19, 2012 12:21 pm

OK! Some progress. The cut down version now runs the function “findLCD” when the button is clicked but in a new window with no formatting?

Still some way off getting to the goal of user input but never the less I’m chuffed with my minor success. Trouble is:- it’s Saturday afternoon and the wife wants to go shopping. A bit frustrating but at least I’m going out on a bit of a high (yea! I know, I’m easily pleased)

<!DOCTYPE html>
<html lang="en">
<head>
<title> FINDING THE LCD</title>
<meta charset="utf-8">
<link rel="stylesheet" href="#.css">
<script src="#.js"></script>
<style>
body {background:#ffdab9;font-size:2.5em}
</style>
</head>

<body>

<form>
Enter the first number:<input type="button" value="click me" onClick="findLCD()" >
</form>

<script>
/* x, y and z are the numbers we want to find the lcd of and - MOST IMPORTANTLY - show the process of the calculation*/
var x = 21
var y = 15
var z = 12
var a = 2
var b = 3
var c = 5
var d = 7
var e = 11 //Not used yet
var f = 13 // ditto
var g = 17 // ditto
var h = 19 /* anything past here is probably not usful in the real world and, anyway, the main idea is to show the process of obtaining the lcd */
var m1=1
var m2=1
var m3=1
var m4=1
var m5=1
var m6=1
var m7=1
var m8=1
var m9=1
var ans=1
/* I'm guessing that I have to make the code from here a function called findLDC but - as yet - I'm not getting the form and the function to marry */

function findLCD(){
while (a>1)
{
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |_____" +x +"_____" +y +"_____" +z +"<br />")
m1=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m2=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m3=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m4=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans+"<br />" )
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m5=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}
//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m6=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m7=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m8=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

}
}
</script>

</body>
</html>
User avatar

merlin

  • Posts: 44
  • Joined: Sun Dec 19, 2010 3:31 pm

Re: First small step !

by merlin » Sun May 20, 2012 9:11 pm

“Seek and ye shall find”!

Now I can go to bed happy.



<!DOCTYPE html>
<html lang="en">
<head>
<title> FINDING THE LCD</title>
<meta charset="utf-8">
<link rel="stylesheet" href="#.css">
<script src="#.js"></script>
<style>
body {background:#ffdab9;font-size:2.5em}
</style>
</head>

<body>
<form>
Enter first number: <input type="text" name="namex" value="">
<br />
Enter second number:<input type="text" name="namey" value="">
<br />
Enter third number:<input type="text" name="namez" value="">
<br />
<input type="button" value="calculate" onClick="findLCD(namex.value,namey.value,namez.value)">
</form>

<script>
function findLCD(n1,n2,n3){
var x = n1
var y = n2
var z = n3
var a = 2
var b = 3
var c = 5
var d = 7
var e = 11
var f = 13
var g = 17
var h = 19
var m1=1
var m2=1
var m3=1
var m4=1
var m5=1
var m6=1
var m7=1
var m8=1
var m9=1
var ans=1

while (a>1)
{
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |_____" +x +"_____" +y +"_____" +z +"<br />")
m1=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m2=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//------------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m3=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//-----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m4=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans+"<br />" )
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m5=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}
//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m6=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m7=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

//----------------------------------------

if (x%a==0){x=x/a}
if (y%a==0){y=y/a}
if (z%a==0){z=z/a}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==a)
{a=b}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==b)
{a=c}
if (x%a!=0 && y%a!=0 && z%a!=0 && a==c)
{a=d}
if (x==1 && y==1 && z==1)
{a=1}
document.write (a +" |______" +x +"_____" +y +"_____" +z
+"<br />")
m8=a
if (a==1)
{
ans = m1*m2*m3*m4*m5*m6*m7*m8*m9
document.write(" the LCD = " +ans +"<br />")
}
if (a==1)
{break;}

}
}
</script>

</body>
</html>
User avatar

merlin

  • Posts: 44
  • Joined: Sun Dec 19, 2010 3:31 pm


+ Post a reply

Page 1 of 1