www.webdeveloper.com
Results 1 to 5 of 5

Thread: Converting SQL script to JavaScript

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    Converting SQL script to JavaScript

    Hi, so I will be undergoing a programming test tomorrow for a project interview in my company.

    Beyond doing a basic training in JavaScript, the content of which should be similar to the JS Basics in w3schools, I havent really worked purely in this langauage. Instead I have done a fair bit of ExtJS, java and a bit of jQuery too.

    My interviewers are aware of this and have given me a task to perform in JavaScript tomorrow for me to prepare for.

    They've told me to: "Converte SQL script to JavaScript". That's all. They will provide the SQL script and I have to convert it to JavaScript. No PHP/Ajax/server side language.

    Can you please give me some relevant information or website that can point me in the right direction? Thank you for looking into this!

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    If I understand the question correctly, you somehow need to manipulate or manage data within JavaScript the same way a database would interpret a SQL statement. If that is correct, then here is how I feel it could be done.

    Using a SQL statement such as:

    Code:
    SELECT * FROM users WHERE active = 1 OR pass = "789" ORDER BY name LIMIT 3
    The JavaScript:

    Code:
    <script type="text/javascript">
    
    	//FROM users
    	var
    		users = [
    			{id : 1, name :   'John', pass : '123', active : 1},
    			{id : 2, name :  'Larry', pass : '456', active : 1},
    			{id : 3, name :   'Jane', pass : '789', active : 0},
    			{id : 4, name :  'Peter', pass : 'abc', active : 1},
    			{id : 5, name :   'Mary', pass : 'def', active : 0},
    			{id : 6, name :  'Harry', pass : 'ghi', active : 1},
    			{id : 7, name :  'Angie', pass : 'jkl', active : 0},
    			{id : 8, name : 'Glenda', pass : 'mno', active : 0},
    			{id : 9, name :    'Bob', pass : 'pqr', active : 1}
    		],
    		recordset = [], i, j;
    
    	//WHERE active = 1 OR pass = "789"
    	for (i in users) {if (users.hasOwnProperty(i)) {
    		if (users[i].active === 1 || users[i].pass === '789') {
    			recordset.push(users[i]);
    		}
    	}}
    
    	//ORDER BY name
    	recordset.sort(function(a, b)
    	{
    		if (a.name.toLowerCase() < b.name.toLowerCase()) {return -1;} else
    		if (a.name.toLowerCase() > b.name.toLowerCase()) {return  1;} else
    		                                                 {return  0;}
    	});
    
    	//LIMIT 3
    	recordset = recordset.slice(0, 3);
    
    	//OUTPUT TO SCREEN
    	document.write('<table border="1">');
    	for (i = 0; i < recordset.length; ++i) {
    		if (!i) {
    			document.write('<tr>');
    			for (j in recordset[i]) {if (recordset[i].hasOwnProperty(j)) {document.write('<th>' + j + '</th>');}}
    			document.write('</tr>');
    		}
    		document.write('<tr>');
    		for (j in recordset[i]) {if (recordset[i].hasOwnProperty(j)) {document.write('<td>' + recordset[i][j] + '</td>');}}
    		document.write('</tr>');
    	}
    	document.write('</table>');
    
    </script>
    The actual output is in a table, but this is a rough version:

    Code:
    id	name	pass	active
    9	Bob	pqr	1
    6	Harry	ghi	1
    3	Jane	789	0
    Last edited by bionoid; 11-21-2013 at 07:45 AM.
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    Thanks for that! I think you're right, this must be how they'll phrase a question. Thanks for taking the time and typing out all this!

    Quote Originally Posted by bionoid View Post
    If I understand the question correctly, you somehow need to manipulate or manage data within JavaScript the same way a database would interpret a SQL statement. If that is correct, then here is how I feel it could be done.

    Using a SQL statement such as:

    Code:
    SELECT * FROM users WHERE active = 1 OR pass = "789" ORDER BY name LIMIT 3
    The JavaScript:

    Code:
    <script type="text/javascript">
    
    	//FROM users
    	var
    		users = [
    			{id : 1, name :   'John', pass : '123', active : 1},
    			{id : 2, name :  'Larry', pass : '456', active : 1},
    			{id : 3, name :   'Jane', pass : '789', active : 0},
    			{id : 4, name :  'Peter', pass : 'abc', active : 1},
    			{id : 5, name :   'Mary', pass : 'def', active : 0},
    			{id : 6, name :  'Harry', pass : 'ghi', active : 1},
    			{id : 7, name :  'Angie', pass : 'jkl', active : 0},
    			{id : 8, name : 'Glenda', pass : 'mno', active : 0},
    			{id : 9, name :    'Bob', pass : 'pqr', active : 1}
    		],
    		recordset = [], i, j;
    
    	//WHERE active = 1 OR pass = "789"
    	for (i in users) {if (users.hasOwnProperty(i)) {
    		if (users[i].active === 1 || users[i].pass === '789') {
    			recordset.push(users[i]);
    		}
    	}}
    
    	//ORDER BY name
    	recordset.sort(function(a, b)
    	{
    		if (a.name.toLowerCase() < b.name.toLowerCase()) {return -1;} else
    		if (a.name.toLowerCase() > b.name.toLowerCase()) {return  1;} else
    		                                                 {return  0;}
    	});
    
    	//LIMIT 3
    	recordset = recordset.slice(0, 3);
    
    	//OUTPUT TO SCREEN
    	document.write('<table border="1">');
    	for (i = 0; i < recordset.length; ++i) {
    		if (!i) {
    			document.write('<tr>');
    			for (j in recordset[i]) {if (recordset[i].hasOwnProperty(j)) {document.write('<th>' + j + '</th>');}}
    			document.write('</tr>');
    		}
    		document.write('<tr>');
    		for (j in recordset[i]) {if (recordset[i].hasOwnProperty(j)) {document.write('<td>' + recordset[i][j] + '</td>');}}
    		document.write('</tr>');
    	}
    	document.write('</table>');
    
    </script>
    The actual output is in a table, but this is a rough version:

    Code:
    id	name	pass	active
    9	Bob	pqr	1
    6	Harry	ghi	1
    3	Jane	789	0

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    I would be very interested to know how things turn out, what was the question and what kind of results they were looking for.

    Good luck for your interview
    JavaScript: Learn | Validate | Compact

  5. #5
    Join Date
    Nov 2013
    Posts
    3
    You were right on the ball, my friend! Thanks for that.

    The queries given were too many and included all kind of things from ROW_NUMBER () OVER (PARTITION BY, INNER JOIN, STRFTIME_USEC(ABC_Campaigns.campaign_expire_date * 1000, '%Y-%m-%d') > TODAY etc to name a few.

    They had actually give a large chunk of their current working code and asked me to convert it to JS.

    I could only do a bit and left out many things like ROW_NUMBER () OVER (PARTITION BY and STRFTIME_USEC which I did not even have time to research and understand. I had only 1.5 hours which was no where near enough the content they wanted me to cover.

    With all that said, I did my best and gave most of my code in comments as otherwise it would not run. They later told me that they did not expect a working html file or for me to complete the whole thing. They just wanted too see how I'd attempt to convert it.

    Lets see what happens. Either ways, thanks a lot for your help!

    Quote Originally Posted by bionoid View Post
    I would be very interested to know how things turn out, what was the question and what kind of results they were looking for.

    Good luck for your interview

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles