Search This Blog

2026/10/15

Javascript:Check numbers are co prime using divisor list

 



function getDivisor(num) {
    let divisor = [];

    for (let i = 2; i <= num; i++) {   // fixed: was i <= num - 1
        if (num % i === 0) {
            divisor.push(i);
        }
    }

    return divisor;
}

function areCoPrime(num1, num2) {
    console.log(`First Number:${num1} Second Number ${num2}`);

    let divisor1 = getDivisor(num1);
    let divisor2 = getDivisor(num2);

    console.log("Divisor", divisor1);
    console.log("Divisor", divisor2);

    let common = divisor1.filter(num => divisor2.includes(num));

    if (common.length > 0) {
        return "Pair is not co-prime";
    } else {
        return "Pair is co-prime";
    }
}

console.log(areCoPrime(6, 9));
// Pair is not co-prime

console.log(areCoPrime(8, 15));
// Pair is co-prime

console.log(areCoPrime(7, 11));
// Pair is co-prime

console.log(areCoPrime(5, 10));
// Pair is not co-prime (this one was wrong before)

console.log(areCoPrime(7, 7));
// Pair is not co-prime (this one was wrong before)

Javascript:Check numbers are co prime using gcd


function gcd(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  while (b !== 0) {
    [a, b] = [b, a % b];
  }
  return a;
}

function areCoPrime(num1, num2) {
  if (!Number.isInteger(num1) || !Number.isInteger(num2)) {
    throw new TypeError("Both arguments must be integers");
  }
  return gcd(num1, num2) === 1;
}

console.log(areCoPrime(6, 9));   // false (gcd = 3)
console.log(areCoPrime(8, 15));  // true  (gcd = 1)
console.log(areCoPrime(7, 11));  // true  (gcd = 1)
console.log(areCoPrime(5, 10));  // false (gcd = 5, the case your old code got wrong)
console.log(areCoPrime(7, 7));   // false (gcd = 7)




EC2 vs Lambda

 


Feature                 EC2                                         Lambda
Server management       You manage it                               AWS manages it
Execution               Continuous or on demand                     Event/request triggered
Scaling                 Configure or use Auto Scaling               Automatic
Billing                 Instance running time, plus other charges   Requests and execution duration, plus other charges
Maximum                 execution time  No fixed                    Lambda-style limit  15 minutes per invocation
OS access               Full control over the VM                    No full underlying server access
Best for                Full backend applications                   Short-lived functions and event-driven workloads

Javascript:Fetch

 


Using fetch to make api call.

Code:

const link = "https://jsonplaceholder.typicode.com/posts/1"


fetch(link).then((res)=>{
   return res.json()
}).then((data)=>{
   console.log(data)
})



Output:
{
  userId: 1,
  id: 1,
  title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
  body: 'quia et suscipit\n' +
    'suscipit recusandae consequuntur expedita et cum\n' +
    'reprehenderit molestiae ut ut quas totam\n' +
    'nostrum rerum est autem sunt rem eveniet architecto'
}



Now lets explore how to make post request.

//post request

const linkPost = "https://jsonplaceholder.typicode.com/posts";


fetch(linkPost, {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        title: "JavaScript",
        body: "Learning fetch POST call",
        userId: 1
    })
}).then((res)=>{
   return res.json()
}).then((data)=>{
   console.log(data)
})

Output:
{
  title: 'JavaScript',
  body: 'Learning fetch POST call',
  userId: 1,
  id: 101
}

Javascript: delete Operator

 



Guess output of following code.



const name ="India"
capital = "New Delhi"

console.log(delete name);
console.log(delete capital);



Explanation:

delete operator delete property from global object,it will not delete variable
declaared by var ,let,const.so output will be

false
true

Javascript: Array Sorting Guess output of the Code




Guess output of code.


Code

let arr =[1,21,30,4]
arr.sort((a,b)=>{
 return b-a
})
console.log(arr)




Here sorting will be done element are treated as number & sorting is descending.
output will be [ 30, 21, 4, 1 ]


let arr =[1,21,30,4]
arr.sort()
console.log(arr)


Here sorting will be done in lexographic order ascending where each element is trated as string

output will be [1,21,30,4]