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