Hackerrank Nodejs
Hackerrank Nodejs
Hackerrank Nodejs
argv
var sum = 0;
for (i = 2; i < process.argv.length; i++) {
sum += Number(process.argv[i]);
}
console.log(sum);
2. TIMER 1
setInterval(function() {
console.log("TCS");
}, 5000);
3. TIMER 2
setTimeout(function() {
console.log("TCS");
}, 2000);
var sum = 0;
for (var x = 0; x < 1000; x++)
{
if (x % 3 === 0 || x % 5 === 0)
{
sum += x;
}
}
console.log(sum);
5. Create a Node.js app which contains two modules; one named App and
the other named modules, which adds and multiplies two given numbers
as input. Use readline core module for i/o
Fibonacci Series
function solution()
{
var fibno = [ 1, 2 ], sum = 0;
function cal(arr )
{
return arr[ arr.length - 1 ] + arr[ arr.length - 2 ];
}
fibno.forEach( function(n)
{
if ( n % 2 === 0 )
{
sum += n;
}
});
return sum;
}
console.log(solution())
var divisor = 2;
var number = 600851475143;
while(number > 1){
if(number % divisor === 0){
number /= divisor;
} else {
divisor++;
}
}
console.log(divisor); //the largest prime factor of 600851475143
Palindrome
function largestPalindrome(){
function isPalin(i){
return i.toString() == i.toString().split("").reverse().join("");
}
console.log(largestPalindrome());
function smallestDivisible(limit) {
var i, n = 1;
function isPrime(n) {
var i, limit = Math.ceil(Math.sqrt(n));
// since the main loop generates odd numbers only
//we can start testing primality dividing by 3
for (i = 3; i <= limit; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
for (i = 3; i <= limit; i += 2) {
if (isPrime(i)) {
n *= largestPower(i, limit);
}
}
console.log(smallestDivisible(20));
Sum of Squares
Find difference between sum of squares of the first 100 natural numbers
and square of the sum:
var squareOfSum = 0
var sumOfSquare = 0
var difference = 0
console.log(primeMover(10001));
Pythogorean Triplet:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product
abc.
Sum of Primes
Find the sum of prime numbers below 2 million.
//Findthesumofprimenumbersbelow2million.
function sumPrimes(num) {
const primes = [];
const filteredArr = [];
let i;
let j;
for (i = 2; i <= num; i++) {
if (!filteredArr[i]) {
primes.push(i);
Sum of Multiples
Find the sum of all multiples of 3 and 5 below 1000 node js:
var sum = 0;
for (var x = 0; x < 1000; x++)
{
if (x % 3 === 0 || x % 5 === 0)
{
sum += x;
}
}
console.log(sum);
try {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir)
}
} catch (err) {
console.error(err)
}
fs.writeFile('Node_folder/sample.txt', 'Learn Node FS
module', function (err) {
if (err) throw err;
console.log('File is created successfully.');
});
//respond
response.write('Hello World');
response.end();
});
server.listen(8000);
<DOCTYPE html>
<html>
<body>
<h2>
Welcome!whatwouldyouliketohave
</h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
var fs = require('fs');
var fs = require("fs");
readStream.on("data", function(data) {
console.log(fileSizeInMegabytes);
});
var fs = require("fs");
var data = 'Node.js is an ultimate backend javascript for backend
developement ';
var writeStream = fs.createWriteStream('./Big_data.txt');
Explore Modules
var module=require('./module.js');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const r2 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var exports=module.exports={};
exports.AddNumber=function(a,b)
{
return a+b;
};
exports.MulNumber=function(a,b)
{
return a*b;
};
touch handlers.js
touch routers.js
touch server.js
});
});
});
req.end();
})
req.end()
var handler = require('./Handler.js');
var http = require('http');
var requestListener = handler.handleRequest();
switch (path) {
case '/hi':
response.send('Hi Welcome');
break;
case '/hello':
response.send('Hello Buddy');
break;
default:
response.writeHead(404);
response.write('404 File not found');
response.end();
}
}
}
function smallestDivisible(limit) {
var i, n = 1;
var p, e = 2, largest = n;
largest = p;
e += 1;
return largest;
function isPrime(n) {
if (n % i === 0) {
return false;
return true;
}
if (isPrime(i)) {
n *= largestPower(i, limit);
console.log(smallestDivisible(20));
var fs = require("fs");
var stream = fs.createReadStream('Node-stream-
handson/data_file.txt','utf8',{ highWaterMark: 60000 });
var data = ''
var dataLength = 0;
//usingareadStreamthatwecreatedalready
stream
.on('data', function (chunk) {
dataLength += chunk.length;
data+=chunk;
})
.on('end', function () { //done
console.log(data);
console.log('Thelengthwas:', dataLength);
});
var fs = require("fs");
var stream = fs.createReadStream('Node-stream-
handson/data_file.txt','utf8',{ highWaterMark: 60000 });
var fs = require("fs");
readable.pipe(writable);