GUVI Codekata Javascript

Arun Prakash
3 min readMay 16, 2020

What’s Codekata?

Codekata a series of programs curated by experts from the industry. Practising on Codekata will take your coding skills to next level. We will recommend your profile to recruiting companies based on your performance in Codekata. You can be a better programmer and crack interviews by completing Codekata.

Description of the same video below.

If you are a first timer the below javascript code has 2 parts

  1. to be ignored as a beginner
  2. to be solved as a beginner

Part to be Ignored(till you understand Nodejs):

Can I skip as a newbie?

yes

Line numbers 1 to 9 and 15. Why I need to ignore and what do these lines do ???

Read below

These lines can be ignored

  1. Line 1 : Import a module called readline
  2. Line 2 — 4: Creates and ref for readline
  3. Line 5:Declares a variable with name userInput ( which is an array). This array is the key for you.
  4. Line 6 & 7: Opens the input stream and reads the data and pushes it to UserInput array.
  5. Line 9 & 15:Closes the input stream as the input is over

Part to be solved:

  1. Line 10 : Gets the first line and splits based on space. The result is stored in variable called data which is an array E.g: if the input is 1 2 the data array will be [‘1’,’2'].
  2. Line 11& 12: Get the value from the data array and stored in a & b. The split data will be string so we use parseInt to convert that to integer. If you want just string then don’t need to use parseInt. Now a will have 1 and b will have 2
  3. Line 13:This is the place where you need to write your logic for the problem you choose in codekata.
  4. Line 14:This is the sample to print you result. This can be part of your logic or separate.
1 const readline = require('readline');
2 const inp = readline.createInterface({
3 input: process.stdin
4 });
5 const userInput = [];
6 inp.on("line", (data) => {
7 userInput.push(data);
8 });
9 inp.on("close", () => {
10 var data = userInput[0].split(" ");
11 var a = parseInt(data[0]);
12 var b = parseInt(data[1]);
13
14 console.log(a+b);
15 });

Reading Single line input in JS ( Nodejs)

Eg Add 2 number and print

Input:

1 2

Output:

3

const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var data = userInput[0].split(" ");
var a = parseInt(data[0]);
var b = parseInt(data[1]);

console.log(a+b);
});

Eg 3:

You are provided with two numbers. Find and print the smaller number.

Sample Input :
23 1

Sample Output :
1

const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var data = userInput[0].split(" ");
var a = parseInt(data[0]);
var b = parseInt(data[1]);

if(a>b)
{
console.log(b);
}else
{
console.log(a);
}
});

Reading Multi-line input in JS ( Nodejs)

You are provided with two numbers. Find and print the smaller number.

Sample Input :
23
1

Sample Output :
1

const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var a = parseInt(userInput[0]);
var b = parseInt(userInput[1]);

if(a>b)
{
console.log(b);
}else
{
console.log(a);
}
});

--

--