Jshacker

Jshacker Java Script Hacker

14/01/2019

Sorted array find a pair of sum

function arrayPairSum(arr, k){
let left =0; let right = arr.length -1;
let result = [];
while(left < right){
let sum = arr[left] + arr[right];
console.log(sum);
if(k == sum){
result = [arr[left], arr[right]];
left+=1;
}else if(k > sum){
left+=1;
}else{
right-=1;
}
}
return result;
}

arrayPairSum([1,2,3,5,6,7,8,11,15],13); // [6,7]

Write a function to reverse a positive integer?function reverseNumber(num){  let rev = 0;  let rem;  while(num > 0){    ...
07/09/2018

Write a function to reverse a positive integer?

function reverseNumber(num){
let rev = 0;
let rem;

while(num > 0){
rem = num % 10;
rev = rev * 10 + rem;
num = Math.floor(num/10);
}

return rev
}

console.log(reverseNumber(123)); // output: 321😉

Java Script Hacker

05/09/2018

Write a function to merge intervals.

Story:
Your company built an in-house calendar tool called HiCal. You want to add a feature to see the times in a day when everyone is available.

You receive the array of meetings with startTime, endTime and now you need to merge the intervals between meetings and show the end result.

Ex: Meetings Array : [{ startTime: 0, endTime: 1 }, { startTime: 3, endTime: 5 }, { startTime: 4, endTime: 8 }, { startTime: 10, endTime: 12 }, { startTime: 9, endTime: 10 }]

your function should return : [ { startTime: 0, endTime: 1 },
{ startTime: 3, endTime: 8 },
{ startTime: 9, endTime: 12 } ];

Answer :

var meetings = [{ startTime: 0, endTime: 1 }, { startTime: 3, endTime: 5 }, { startTime: 4, endTime: 8 }, { startTime: 10, endTime: 12 }, { startTime: 9, endTime: 10 }]

function merge_overlaping_intervals(arr) {
// if (arr.length a.startTime > b.startTime);
let result = [];
result.push(s[0]);

for (let i = 1; i < s.length; i++) {
let top = result[result.length - 1];
if (top.endTime < s[i].startTime) {
result.push(s[i]);
} else if (top.endTime < s[i].endTime) {
top.endTime = s[i].endTime;
}
}

return result;
}

console.log(merge_overlaping_intervals(meetings));

01/09/2018

Write a recursive function to sum the elements of an array?

function arrSum(arr) {
if (arr.length == 1) return arr[0];
return arr[0] + arrSum(arr.slice(1));
}

console.log(arrSum([10, 2, 3, 1, 4]));

Bubble Sort in JavaScript: Big O(n*n)
05/06/2018

Bubble Sort in JavaScript: Big O(n*n)

Problem: Print a given matrix in a spiral formSolution:
31/05/2018

Problem: Print a given matrix in a spiral form
Solution:

Problem:Find a combination of the numbers in a unsorted array which are equals to sum using JS.Solution:
18/08/2017

Problem:
Find a combination of the numbers in a unsorted array which are equals to sum using JS.

Solution:

13/03/2016

JavaScript programmers like to use words like, “event-loop”, “non-blocking”, “callback”, “asynchronous”, “single-threaded” and “concurrency”. We say things l...

04/11/2015

Q : Print A to Z alphabets without using loops.

var printAlphabets = function(c){
c = c || 97; // 97 is a
if(c < 123){
console.log(String.fromCharCode(c++));
printAlphabets(c);
}
}

26/09/2015

Find non repeated number in an array

var ar = [1,2,3,4,5,5,4,3,1],
num = 0;

for(var i =0; i

26/09/2015

Promise visualization playground for the adventurous

Address

Hyderabad

Website

Alerts

Be the first to know and let us send you an email when Jshacker posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Share