Java Script -Build a Table – Exam Problems

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 🙂

SoftUni

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 startend.
    • Allowed working time for your program: 0.2 seconds.
    • Allowed memory: 16 MB.

Examples

Input

Output

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:

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:

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.

😀

About

A VBA Developer.

Tagged with: , ,