I found limited information while searching about interveiw of HCL Technology, so I writed this to feedback to LeetCode community.
Position: NodeJS Developer for a new team
Time: 45m
Comment: The questions are simple, and they would invite you to open your webcam and share your screen.
【Round1】
Q1: Write a function: sumDigProd(a, b) with only two parameters as the input, until the result has only one digit.
ex:
sumDigProd(16, 28)
-> 16 + 28 =44
-> 4 * 4 = 16
-> 1 * 6 = 6
and return 6My solution in JS
function sumOfThisDig(N) {
str_N = N.toString();
result = 1;
for (let i = 0; i < str_N.length; i++) {
result *= Number(str_N[i]);
};
return result.toString();
}
function sumDigProd(a, b) {
sum = a + b;
sum = sum.toString();
while (sum.length > 1) {
sum = sumOfThisDig(sum);
}
return sum;
}
console.log(sumDigProd(16, 28))Q2. Write a server and show 'Hello' after sending url in the browser.
ex:
http://localhost:4000/greet/
show "Hello"And here's my solution.
var express = require('express')
let app = express()
app.get('/greet/', function(req, res) {
res.end('Hello!');
});
var server = app.listen(4000, () => {
const host = server.address().address;
});Q3. Write a server and show 'Hello ${name}' after sending url in the browser.
ex:
http://localhost:4000/greet/Sandy
show "Hello Sandy!"I only add this code after Q2
app.get('/greet/:name', function(req, res) {
res.end('Hello! ' + req.params.name);
});Q4. How to add header for all response with add it manually in each api function?
This is straightforward to apply next(), so I add this code after Q3.
app.get('/*', function(req, res, next) {
res.setHeader('Content-Type','application/json');
next();
})Finish, and proceed to Round 2.