javascript - why this code didn't work correctly? -
i have following problem resolve, don't understand why code doesn't work correctly.
write javascript function takes input array of 2 numbers (start , end) , prints @ console html table of 3 columns.
- column 1 should hold number
num
, changing start end.- column 2 should hold
num*num
.- columns 3 should hold "yes" if
num
fibonacci number or "no" otherwise.the table should have header cells titled "num", "square" , "fib". see below examples.
input
input data comes array of 2 numbers: start , end. input data valid , in format described. there no need check explicitly.
output
print @ console above described table in same format examples below. don't add additional spaces. whitespace , character casing important, please use same in below examples.
constraints
• input passed first javascript function found in code array of 2 elements.
• numbers start , end positive integers in range [1…1 000 000] , start ≤ end.
• allowed working time program: 0.2 seconds.
• allowed memory: 16 mb.
this code not same requirement of problem, idea same guess.
here code:
var fib = []; var a, b, result; = 0; b = 1; result = b; (var = 1; < 31; i++) { result = + b; = b; b = result; fib.push(result); } console.log("<table>"); console.log("<tr><th>num</th><th>square</th><th>fib</th></tr>"); var start = 2; var end = 6; function isfib(start, end) { (i = start; < end; i++) { fib.foreach(function (element) { if (i === element) { return "yes"; } else { return "no"; } }); } } function buildtable() { for(var j = start; j < end; j++) { console.log("<tr><td>" + j + "</td><td>" + j * j + "</td><td>" + isfib(start, end) + "</td></tr>"); } } buildtable(start, end);
this code not same requirement of problem, idea same guess.
this kind of feels doing 1 of homework assignments here go.
var = [1,25]; function fib(n){ return n<2?n:fib(n-1)+fib(n-2); } function isfib(n){ var trynum = 0; { var fibnum = fib(trynum++); } while (fibnum < n); return (fibnum==n); } function printtable(a){ var table = "<table><thead><tr><td>num</td><td>square</td><td>fib</td></tr></thead>"; for(var i=a[0]; i<=a[1]; i++){ table += "<tr>"; table += "<td>"+i+"</td>"; table += "<td>"+(i*i)+"</td>"; table += "<td>"+((isfib(i))?"yes":"no")+"</td>"; table += "</tr>"; } table += "</table>"; return table; } document.write( printtable(a) );
table td { border: 1px solid black; } thead { background: #eee; }
here improved isfib
function stores old values faster. still takes 1 second calculate fibs 1 1,000,000 way faster before. if try print table 1,000,000 records in dom crash browser dont bother trying.
var fibs = [1, 2]; function isfib(n) { if (fibs[fibs.length - 1] < n) { // not calculated n yet { fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]); // next fib } while (fibs[fibs.length - 1] < n); // until found (or above) n } return (fibs.indexof(n) > -1); // n in list of fibs? }
last time optimized speed , got dramatic speed increase (still not fast enough requirements). attempt optimizes memory because ever stores last 2 fibs, fast, runs in 1 second.
function printtable(a){ var previousfib = 1; var currentfib = 2; function nextfib(){ var temp = currentfib; currentfib += previousfib; previousfib = temp; } var table = "<table><thead><tr><td>num</td><td>square</td><td>fib</td></tr></thead>"; for(var i=a[0]; i<a[1]; i++){ table += "<tr>"; table += "<td>"+i+"</td>"; table += "<td>"+(i*i)+"</td>"; table += "<td>"; if(i==0||i==1) table += "yes"; else { while (currentfib < i){ nextfib(); console.log(currentfib); } table += (currentfib==i)?"yes":"no"; } table += "</td>"; table += "</tr>"; } table += "</table>"; return table; }
Comments
Post a Comment