On 27. July I have participated in an exam organized by the SoftUni for Java Script. This time, I managed to get 220 points out of 400, which ranked me in 58th place out of 204 participants for the day and in total 138th out of 442 participants, but who is counting actually 🙂
So, in order to obtain 220 points I managed to resolve successfully 2 Java Script problems. In this article, I will present out of them – it is called “Build a Table” and this is its description:
Problem 1 – Build a Table
Write a JavaScript function that takes as input an array of two numbers (start and end) and prints at the console an 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“. See the below examples.
Input
The input data comes as array of two numbers: start and end. The input data will always be valid and in the format described. There is no need to check it explicitly.
Output
Print at the console the above described table in the same format like the examples below. Don’t add additional spaces. Whitespace and character casing are important, so please use the same as in the below examples.
Constraints
- The input is passed to the first JavaScript function found in your code as array of 2 elements.
- The numbers start and end are positive integers in the range [1…1 000 000] and start ≤ end.
- Allowed working time for your program: 0.2 seconds.
- Allowed memory: 16 MB.
Examples
Input |
Output |
2
6 |
<table>
<tr><th>Num</th><th>Square</th><th>Fib</th></tr> <tr><td>2</td><td>4</td><td>yes</td></tr> <tr><td>3</td><td>9</td><td>yes</td></tr> <tr><td>4</td><td>16</td><td>no</td></tr> <tr><td>5</td><td>25</td><td>yes</td></tr> <tr><td>6</td><td>36</td><td>no</td></tr> </table> |
So, what I did?
At first I tought of it as a pretty easy challenge – once you are able to read the input, it seems that the task is quite trivial. Actually, this is not the case. The 3rd column required some additional knowledge for building functions in Java Script. For this additional knowledge I needed some 90 minutes in order to bring it “to life”. At the end, this was my result, which gave me 100 % of the points:
1 2 3 4 5 6 7 8 |
function solve(input) { var start = parseInt(input[0]); var end = parseInt(input[1]); var firstLine = ""; var secondLine = ""; var lastLine = "num square fib<span style="background-color: #f4f4f4;">";</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
console.log(firstLine); console.log(secondLine); var middle1 = ""; var middle2 = ""; var middle3 = ""; var numberOfLines = end-start+1; for (var a = 0; a < numberOfLines; a++) { var temporaryLog = ""; function isFibonacci(start) { return ((isPerfectSquare(5*start*start + 4)) || (isPerfectSquare(5*start*start - 4))); } function isPerfectSquare(x){ var s = Math.floor(Math.sqrt(x)); return ((s*s)===x) console.log ((s*s)===x) } var isFib = ""; if(isFibonacci(start)){ isFib = "yes"; }else{ isFib = "no"; } temporaryLog = middle1 + start + middle2 + start*start + middle2 + isFib + middle3; start++; console.log(temporaryLog); } console.log(lastLine); } |
It worked like a charm! 🙂 Actually, the more I look at it, the more I like it 🙂
The original solution is actually not shorter, which comes to think that mine is not that bad. They have also used two functions, combining the two for Fibonacci in one and adding an additional one for printing a row. This is how the professionally written one looks like:
1 2 3 4 5 6 7 8 9 10 11 12 |
function printTableOfNumbers(input) { var startNum = Number(input[0]); var endNum = Number(input[1]); fibNums = calcFibonacciNums(endNum); console.log(''); console.log(''); for (var num = startNum; num <= endNum; num++) { var numSquare = num * num; var isFibonacci = fibNums[num] ? "yes" : "no"; printAsTableRow(num, numSquare, isFibonacci); } console.log('num square fib |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
'); function calcFibonacciNums(maxNum) { var fibNums = { 1: true }; var f1 = 1; var f2 = 1; while (true) { var f3 = f1 + f2; if (f3 > maxNum) { return fibNums; } fibNums[f3] = true; f1 = f2; f2 = f3; } } function printAsTableRow() { var tableRow = ''; for (var i in arguments) { tableRow += '' + arguments[i] + ''; } tableRow += ''; console.log(tableRow); } } // ------------------------------------------------------------ // Read the input from the console as array and process it // Remove all below code before submitting to the judge system! // ------------------------------------------------------------ var arr = []; require('readline').createInterface({ input: process.stdin, output: process.stdout }).on('line', function (line) { arr.push(line); }).on('close', function () { printTableOfNumbers(arr); }); |
Edit: Due to some escaping chaacter in the code, it is automatically split into two parts. Quite unprofessional, but sometimes it happens. Anyway, you must grasp the idea, if you go line by line.
😀