Javascript Challenge

Pradeepa Kathiresan
3 min readOct 28, 2020

www.hackerrank.com

Live Demo

'use strict';const fs = require('fs');process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'findNumber' function below.
*
* The function is expected to return a STRING.
* The function accepts following parameters:
* 1. INTEGER_ARRAY arr
* 2. INTEGER k
*/
function findNumber(arr, k) {
// Write your code here
let result = "NO";
for (let i = 0; i < arr.length; i++) {
console.log("arr[i] = ", arr[i]);
if (arr[i] == k) {
result = "YES";
}
else {
console.log("Not found yet");
}
}
return result;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const arrCount = parseInt(readLine().trim(), 10);let arr = [];for (let i = 0; i < arrCount; i++) {
const arrItem = parseInt(readLine().trim(), 10);
arr.push(arrItem);
}
const k = parseInt(readLine().trim(), 10);const result = findNumber(arr, k);ws.write(result + '\n');ws.end();
}
'use strict';const fs = require('fs');process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'oddNumbers' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER l
* 2. INTEGER r
*/
function oddNumbers(l, r) {
// Write your code here
let result = [];
for (let i = l; i <= r; i++) {
if (i%2 == 1) {
//console.log(i, " = ODD");
console.log(i);
result.push(i);
} else {
//console.log(i, " = EVEN");
}
}
return result;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const l = parseInt(readLine().trim(), 10);const r = parseInt(readLine().trim(), 10);const result = oddNumbers(l, r);ws.write(result.join('\n') + '\n');ws.end();
}for (let i = 0; i < arrCount; i++) {
const arrItem = parseInt(readLine().trim(), 10);
arr.push(arrItem);
}
const k = parseInt(readLine().trim(), 10);const result = findNumber(arr, k);ws.write(result + '\n');ws.end();
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Pradeepa Kathiresan
Pradeepa Kathiresan

Written by Pradeepa Kathiresan

I am open to cross-train and interested in learning new technology.

Responses (2)

Write a response