IPB





Welcome Guest ( Log In | Register )

> Need some help from Javascript and SQL peeps, I have an assesment test I have been working on
fido77
post Oct 31 2021, 10:08 PM
Post #1


Knight Lieutenant
********

Group: Clan Members
Posts: 989
Thank(s): 1
Points: 215
Joined: 5-June 08
From: Tyler, Texas, US
Member No.: 124




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




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

Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
Sir Robin (not s...
post Nov 1 2021, 01:21 PM
Post #2


Knight Lieutenant
********

Group: Trusted non-SM
Posts: 921
Thank(s): 524
Points: 921
Joined: 9-March 16
From: Bulgaria
Member No.: 4,846




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
Go to the top of the page
 
+Quote Post
The Following 1 Users Say Thank You to Sir Robin (not so brave) For This Useful Post:
fido77

Posts in this topic


Reply to this topicStart new topic

 



RSS Lo-Fi Version Time is now: 19th September 2024 - 11:11 PM
Sneaky Monkeys Clan :: MonkeyFiend.com