homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
display fibonacci sequence on web page
really stuck
gunter




msg:3939298
 8:04 am on Jun 24, 2009 (gmt 0)

Can anyone please write, solve the program for displaying the following on a web page.

The first 20 elements of the fibonacci sequence are,
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765
9 ratios were needed
the final ratio was:1.6176470588235294
the golden ratio is:1.61803399
the final difference was:0.00038693117647059516

the program must be written with the following information.
first write the function called difference() that will take two numbers as arguments and return the difference between them.
Then generate the first 20 numbers for the fibonacci sequence and store them in an array called fibonacciArray.
Then it should calculate the ratio by dividing each element with the one before it and store these values in a second parrallel array called ratioArray.
It should then find the first value of ratioArray that is closer to the golden ratio than 0.0001m byt 1.61803399 will be used as the value for the golden ratio.
finally it should display output in browser similar to what is above.
thankyou very much, any help will be greatly appreciated

 

whoisgregg




msg:3940423
 6:46 pm on Jun 25, 2009 (gmt 0)

Welcome to WebmasterWorld, gunter!

We'd be happy to help you troubleshoot code you've already started. We are all volunteers here, so we rarely have time to write complete scripts for other people.

However, if you do get the code started and post what you've got, I'm sure we'll be able to help. :)

gunter




msg:3940445
 7:28 pm on Jun 25, 2009 (gmt 0)

this is what i have tried so far, but nothing happens.

<html>

<head>

<title></title>

</head>

<body>
function difference()
{
{document.write('difference' + '/' + '<BR>');
}

var fibArray(20);

int i, fib[20]#include<stdio.h>

int main() {

fib[1}= 0;

fib[1] = 1;
{
for (i = 2; i < 10; i++) {fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The fibonacci series is as follows \n");
for (i = 0; i < 10; i++) {

printf("%d \n", fib[i]);

}
return 0;

}

</body>

</html>

birdbrain




msg:3940521
 9:25 pm on Jun 25, 2009 (gmt 0)

Hi there gunter,

I do have a fibonacci sequence generator, that I coded, on my pc, would that help you in your project?

bidbrain

pinterface




msg:3940547
 10:09 pm on Jun 25, 2009 (gmt 0)

Your code there has a mix of HTML, JavaScript, and C--not one of which is correct. That's a fairly significant misunderstanding which is almost certainly best solved by finding your instructor and asking for help.

It's okay! That's what instructors are for!

If you don't want to ask your instructor--or your instructor is not particularly helpful--you could also try searching the web for [javascript tutorial] or looking for beginner-level JavaScript books at your local bookstore or library.

gunter




msg:3940679
 5:32 am on Jun 26, 2009 (gmt 0)

that would be great birdbrain, thankyou.

i have tried again, is this any improvemt, though, it still don`t work lol.

<HTML>
<HEAD>
<TITLE>Fibonnaci Sequence
</TITLE>
<SCRIPT>

/************************* Function definition *************************/
function return;
function difference();
{
document.write('take < number' / 'by > number','');
}

var fibonacciArray = [];
var ratioArray = [];

var firstnumber = 1, secondnumber = 1, thirdnumber = 0;

while (thirdnumber < 20000)
{
thirdnumber = firstnumber + secondnumber;
if (thirdnumber < 20000)
{
return(thirdnumber);
}
firstnumber = secondnumber;
secondnumber = thirdnumber;
}
Console.ReadLine();

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

birdbrain




msg:3940705
 6:39 am on Jun 26, 2009 (gmt 0)

Hi there gunter,

here is my little fibonacci sequence generator...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>fibonacci numbers</title>

<style type="text/css">
body {
font-family:verdana,arial,sans-serif;
color:#000;
}
#container{
width:240px;
font-size:14px;
background-color:#f0f0f0;
border:3px double #999;
margin:30px auto;
}
#container input {
width:80px;
margin:5px 0 5px 10px;
}
#t0,#t1,#t2 {
border:1px solid #999;
}
.but {
background-color:#dfdfdf;
border-color:#ccc;
}
#fibo {
width:600px;
padding:10px;
margin:10px auto;
border:3px double #999;
background-color:#eee;
font-family:courier,monospace;
font-size:8px;
line-height:30px;
text-align:justify;
}
.hide {
display:none;
}
h1 {
margin-top:30px;
font-size:16px;
text-align:center;
}
</style>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}

function init(){
obj=document.getElementById('fibo');
fib=[];
df=document.forms[0];
df.className='';
df[0].focus();
document.getElementById('h').className='';

df[3].onclick=function() {
fibonacci();
}
df[4].onclick=function() {
tidyup();
}
}
function fibonacci () {
if((isNaN(df[0].value))[red][/red](df[0].value=='')) {
alert('1st number required');
df[0].value='';
df[0].focus();
return;
}
if((isNaN(df[1].value))[red][/red](df[1].value=='')) {
alert('2nd number required');
df[1].value='';
df[1].focus();
return;
}
if((isNaN(df[2].value))[red][/red](df[2].value=='')[red][/red](df[2].value<3)) {
alert('the fibonacci sequence length must be at least 3');
df[2].value='';
df[2].focus();
return;
}
fib[0]=parseFloat(df[0].value);
fib[1]=parseFloat(df[1].value);
obj.firstChild.nodeValue=fib[0]+', '+fib[1]+', ';
for(c=2;c<df[2].value;c++){
fib[c]=fib[c-1]+fib[c-2];
obj.firstChild.nodeValue+=fib[c]+', ' ;
obj.className='';
}
}
function tidyup() {
obj.firstChild.nodeValue='';
obj.className='hide';
df[0].focus();
}

</script>

</head>
<body>

<h1 id="h" class="hide">fibonacci sequence</h1>

<form action="#" class="hide" >
<div id="container">
<input id="t0" type="text"><label for="t0"> : 1st number</label>
<input id="t1" type="text"><label for="t1" > : 2nd number</label>
<input id="t2" type="text"><label for="t2"> : fibonacci length</label>
<input class="but" type="button" value="calculate">
<input class="but" type="reset" value="clear">
</div>
</form>

<div id="fibo" class="hide">&nbsp;</div>

</body>
</html>

Note:-
You must replace all instances of the broken vertical line -
with a solid vertical line.

Further reading:-


birdbrain

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved