Advertisement
dimipan80

Exams - Build a Table

Nov 13th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function that takes as input an array of two numbers (start and end)
  2.  and prints at the console a HTML table of 3 columns. The first column should hold a number num,
  3.  changing from start to end. The second column should hold num*num. The third column should hold "yes"
  4.  if num is Fibonacci number or "no" otherwise. The table should have header cells titled "Num", "Square" and "Fib". */
  5.  
  6. function tableBuilder(args) {
  7.     console.log("<table>");
  8.     console.log("<tr><th>Num</th><th>Square</th><th>Fib</th></tr>");
  9.     var start = Number(args[0]);
  10.     var end = Number(args[1]);
  11.     var fibonacciSeqNums = [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,
  12.         4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040
  13.     ];
  14.  
  15.     for (var i = start; i <= end; i += 1) {
  16.         var squareNum = i * i;
  17.         var isFibonacciNum = 'no';
  18.         for (var j = 0; j < fibonacciSeqNums.length; j += 1) {
  19.             if (fibonacciSeqNums[j] == i) {
  20.                 isFibonacciNum = 'yes';
  21.                 break;
  22.             }
  23.         }
  24.  
  25.         console.log("<tr><td>%d</td><td>%d</td><td>%s</td></tr>", i, squareNum, isFibonacciNum);
  26.     }
  27.     console.log("</table>");
  28. }
  29.  
  30. tableBuilder(['2', '6']);
  31. tableBuilder(['55', '56']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement