PDA

View Full Version : Little piece of JS...



AmEv
01-10-2012, 11:43 PM
I needed to work on my final project for my programming class.
Here's an excerpt of JavaScript that you may find useful:

//AmEv' Final project source, 2012
var runs = parseInt(prompt("How many steps do you want?")); //Ask how many steps of the Fibonacci sequence are desired
var x = 1; //X is first Fib num
var y = 0; //Y is second Fib num
var z = 0; //Z is a temp number
var a = 0; //A is successful numbers to display
while(1){ //Start infinite loop
document.write(x+y+"<br />"); //Display number
var z = x+y; //Set 1st and 2nd nums to temp
var x = y; //Set 2nd num to 1st
var y = z; //Set temp num to 2nd
a++; //Add to displayed numbers
if(runs==a){ //Break out of loop
break;
}
} //End infinite loop


It's still in early dev stage, as it's still too simple (for what my teacher needs), but hopefully, I can increase efficiency. Maybe you can do something with it :P

Drum Thumper
01-11-2012, 02:06 AM
Someone else here did some Fibb work a few years ago but I cannot remember who.

AmEv
01-11-2012, 10:25 AM
It DOES produce the Fibb sequence based on steps from the Alert box.

If anyone decides to do it, copy this into a new HTML file:

<!doctype html>
<html>
<head>
<title>Fibonacci sequence
<script type="text/javascript>
(code above)
</script>
<body></body>
<html>

NightrainSrt4
01-11-2012, 05:26 PM
What is the project like? Is this high school or college? Fib work is usually one of the first examples / assignments in college classes.

AmEv
01-11-2012, 06:54 PM
High school.
I took it upon myself to do the project.

diluzio91
01-11-2012, 11:42 PM
I had to do this one too... lol... i miss my old programming classes before everything we had to write was recursive... lol

AmEv
01-12-2012, 12:02 AM
Did you do it this way, DiLuzio?

NightrainSrt4
01-12-2012, 08:53 AM
High school.
I took it upon myself to do the project.

I reread what I wrote, and realized it could have come across as harsh. Really, I was just being inquisitive.

I don't know javascript, but aside from what I assume is just a newline / linebreak, it all looks the same. Unless JS handles while loops differently, if the number of runs the user chooses is zero or negative, the loop should just continue ad infinitum.

NightrainSrt4
01-14-2012, 10:09 AM
I copied it all to make sure I was correct. Did you fix it? A change in your loop condition will stop it from executing on zero or negative numbers. You can also do a check first to see if that is the case and print to the user to choose a positive number, instead of just returning a blank screen.

AmEv
01-14-2012, 07:07 PM
I'm still polishing it.

NightrainSrt4
01-14-2012, 08:58 PM
If you need help with it, or an extra set of eyes, just post it up. I'm not keen on giving answers to things, but as above I can give nudges in the right direction.

AmEv
01-14-2012, 10:13 PM
Here are my teacher's exact directions:

You must use an external JavaScript file and link it to your HTML document
You must make use of one or more variables
You must use at least one built-in function
You must create at least one custom function
You must use at least one selection structure
You must use at least one repetition structure

I'll bold what I (think I) have done.

PS: He's docking so many points (forget how many) for each semicolon not being where it should.

AmEv
01-14-2012, 10:33 PM
My complete code, as of now:

fibb.html:

<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence</title>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<form name="main">
This Web page is designed to display the Fibbonacci sequence based on a user-defined amount of steps. <br />
Because it uses math on a Web page, it has the potential to crash your computer (or, at the very least, your browser).<br />
Please, choose a relatively low number to keep it safe.<br />
I accept <input type="checkbox" name="accept" onclick="accept()" /><br />
<input type="button" name="Run" value="Run" onclick="fibb()" />
</form>
</body>
</html>

main.js:

//Ammon Evans' Final project source, 2012

var a;
var acc;
var x = 1; //X is first Fib num
var y = 0; //Y is second Fib num
var z; //Z is a temp number

function accept(){
switch(form.main.accept.value){
case true:
acc = 1;
break;
default:
acc = 0;
}
}

function fibb(){
var runs = parseInt(prompt("How many steps do you want?")); //Ask how many steps of the Fibonacci sequence are desired
if (acc == 1 & runs >= 0){
var a = 0; //A is successful numbers to display
while(1){ //Start infinite loop
document.write(x+y+"<br />"); //Display number
z = x+y; //Set 1st and 2nd nums to temp
x = y; //Set 2nd num to 1st
y = z; //Set temp num to 2nd
a++; //Add to displayed numbers
if(runs==a){ //Break out of loop
break;
}
} //End infinite loop
}else{
alert("Please choose a positive integer.")
}
}

What I'm doing with the selection structure is to confirm they have the "I agree" check confirmed on-click.

NightrainSrt4
01-15-2012, 09:18 AM
Looks better. Some things stick out to me, but they may be javascript specific so they may just be going over my head.

You declare a global variable a but don't use it, and instead define a local variable a within fibb() and use that. Looks like you can drop the global declaration of a completely, or keep it and just initialize a within fibb and not declare a new variable.

In the accept function you use a switch for something I would have just used an if statement. If form.main.accept.value could return more useable values than just true or false then I would have used the switch, but with just true or false I probably would have used an if statement.

Single letter variable names not used as iterators in a loop. This is probably just how I was taught, and not a huge deal in a small app / program, but I try to always name my variables something that reflects what they are. I will often forget to do that, but if I've learned anything from programming my game, the more lines of code you have to deal with the more important it becomes for readability. I've gone back to something 1k, 5k, 10k lines of code later and looked at something and really wish I had used more descriptive variable names than what I had.

If you don't want the app to run too many times you can put a hard upper cap on the number of runs. Specify it in your description, then just put the condition in the if statement. Change the else response to explain again to the user what the upper cap is. This gets rid of the crashing without relying on the user to make a choice on what number of runs is acceptable.

It looks good. I nitpick when I peer review, but that's the way I've always been. It stems from too many times having my own peer reviews come back with nothing but "good job!" and "perfect!" even though I know there are things I could do better, or a bit differently, all over the place. I'd rather give you too much to think about then give you nothing or too little.

AmEv
01-15-2012, 03:45 PM
And THAT'S why I have an extra set of eyes.

Thanks.

NightrainSrt4
01-15-2012, 03:53 PM
No problem. Glad to be of help.

AmEv
01-15-2012, 04:57 PM
As far as the Switch case, the main reason I am including it is to show him I know how to use it. I personally would prefer the if() statement, but.......

NightrainSrt4
01-15-2012, 05:59 PM
Ahh, I see. Some teachers are like that; who knows if they are in a crappy mood and decide an if statement isn't a good enough selection structure. In that case, they'd probably think the switch was more advanced and give more credit.