Printable Version of Topic

Click here to view this topic in its original format

Sneaky Monkeys Forum _ Public Discussion _ Need some help from Javascript and SQL peeps

Posted by: fido77 Oct 31 2021, 10:08 PM

I have this assessment test I have been working on for my job. I don't mess with JavaScript or SQL very often, but they want me to finish it anyway :/
Was wondering if anyone here, like my good buddy Monkeyfiend, has any experience with it. I'm more of an OOP C# programmer. It looks simple enough, but I'm stuck on the second set of JavaScript questions and the second two SQL questions. I put answers, but I want to be sure they are correct before sending it back Monday morning. I'll just post them here for anyone who wants to give it a shot. Very much appreciated for any assistance.

//JavaScript

let data = ["A","B","C"];
let results = ["1","2","3"];
let entity = {};
for(let i = 0; i < data.length; i++){
entity[data[i]] = results[i];
}
console.log(entity);
Question: What do you expect the console.log will output?
--------------------------------------------------------------------------------------------
let fs = require('fs');

function read() {
var promise = new Promise(function(resolve, reject) {
fs.readFile("myFile.txt", "utf8", (err, data) => {

if (!err) {
resolve(data);
}
else {
reject(err);
}
});
});
return promise;
}


Question: Use the read function and output the results.
read().then((x) => {console.log(x)}).catch((err) => console.log(err) );
----------------------------------------------------------------------------
async / await option
try{
const x = await read();
console.log(x);
}catch(err){
console.log(err);
}
-------------------------------------------------------------------------------
function getText(text){
return new Promise(function(resolve, reject){
setTimeout(function(){
if(text)
resolve(text);
else
reject(new Error("text is empty"))
}, Math.random() * 10);
});
}
function logData(data){
console.log(data);
}

getText("Hello").then(logData);
getText("World").then(logData);

Question: Sometimes this app produces “Hello World” sometimes it produces “World Hello”. Explain why and correct it.
----------------------------------------------------------------------------------------------------------------------------------------

function data (){
return [1,2];
}

let [x,y] = data();

console.log(x);
console.log(y);
Question: What will console log out in this program and why?
________________________________________
_______________

SQL Technical Screening

Books
Id (int)
Title (text)
Price (int)
Authors
Id (int)
Name (text)
BookAuthors
BookId (int)
AuthorId (int)

WRITE A SELECT STATEMENT SHOWING ALL THE BOOKS ORDERED BY TITLE IN ASCENDING ORDER

WRITE A SELECT STATEMENT SHOWING ALL THE BOOKS AND THE BOOK'S RELATED AUTHOR(S)

WRITE A SELECT STATEMENT SHOWING THE AVERAGE COST OF A BOOK BY AUTHOR



Posted by: fido77 Nov 1 2021, 03:49 AM

I almost have all the SQL completed, which seems simple, but the last one I am having a problem with. I'm getting the average of all the prices instead of just the average price of the books by a certain author. Here is what I have wrote:

Select Authors.Name, AVG(Price) AS AVG_Price
From Authors, BookAuthors
INNER JOIN Books ON BookAuthors.BookID = Books.ID
GROUP BY Authors.name;

Posted by: fido77 Nov 1 2021, 04:28 AM

I think I got it smile.gif

Select Authors.Name, AVG(Price) AS AVG_Price
From Authors INNER JOIN BookAuthors ON BookAuthors.AuthorID = Authors.ID
INNER JOIN Books ON BookAuthors.BookID = Books.ID
GROUP BY Authors.name;

That seems to work.

Now, The only part on the Javascript I'm having seems to trouble everyone I've talked to. This is what they gave me:

let fs = require('fs');

function read() {
var promise = new Promise(function(resolve, reject) {
fs.readFile("myFile.txt", "utf8", (err, data) => {

if (!err) {
resolve(data);
}
else {
reject(err);
}
});
});
return promise;
}

------------------------------------------------------------------------------
Question: Use the read function and output the results.

read().then((x) => {console.log(x)}).catch((err) => console.log(err) );
--------------------------------------------------------------------------------
async / await option

try{
const x = await read();
console.log(x);
}catch(err){
console.log(err);
}

-----------------------------------------------------------------------------------

That is what they gave me. I didn't put any answer yet. I'm not sure what to do with this. It's almost like they gave me the problem and the answer, and there is nothing left to do. Am I missing something here?

Posted by: Sir Robin (not so brave) Nov 1 2021, 01:21 PM

My JavaScript-fu is quite weak. I saved the program along with the first answer in t.js and ran it with node t.js.

This produced the following error:

CODE
{ [Error: ENOENT: no such file or directory, open 'myFile.txt'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'myFile.txt' }


Note, I'm on Linux. The error may look different on another system.

I then created the file myFile.txt, with one line of text "Some stuff" and reran the program. This is what I got:
CODE
Some stuff



I then replaced the first answer with the await one and rerun the code. This time I got an error:

CODE
/tmp/fido/t.js:22
const x = await read();
^^^^^

SyntaxError: await is only valid in async function
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)


I'm not sure if they are asking you to select the working answer, or to fix the await one to work too. If it's the latter, you'll need to google how to use javascript await.

HTH

Posted by: fido77 Nov 2 2021, 02:57 AM

I appreciate your time for looking into this Sir Robin! I just sent it back to them and said I believe something is missing, like the file to read from. They said that one of the developers made the assessment test, and that there could be something missing. I guess we'll find out sometime this week smile.gif

Posted by: Sir Robin (not so brave) Nov 9 2021, 04:37 PM

Hey fido. How did it go?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)