Ticker

6/recent/ticker-posts

Javascript Coding

Problem 1. Four Arithmetic Operation with the help of HTML & Javascript 


<!DOCTYPE html>

<html>

<head>

<title>Four function calculators</title>

<script language="javascript">

function Addition()

{

var x,y,c,c1,z,sp, c2,c3;

x=parseInt(document.calculator.x.value);

y=parseInt(document.calculator.y.value);

c=document.getElementById("c");

c1=document.getElementById("c1");

sp=document.getElementById("sp");

c2=document.getElementById("c2");

c3=document.getElementById("c3");

if(c.checked==true)

{

z=x+y;

//alert(z);

sp.innerHTML=z;

c1.checked=false;

c2.checked=false;

c3.checked=false;

}


// body...

}

function Substraction()

{

var x,y,c,c1,z,sp,c2,c3;

x=parseInt(document.calculator.x.value);

y=parseInt(document.calculator.y.value);

c=document.getElementById("c");

c1=document.getElementById("c1");

sp=document.getElementById("sp");

c2=document.getElementById("c2");

c3=document.getElementById("c3");

if(c1.checked==true)

{

z=x-y;

//alert(z);

sp.innerHTML=z;

c.checked=false;

c2.checked=false;

c3.checked=false;



}


// body...

}

function multiplication()

{

var x,y,c,c1,z,sp,c2,c3;

x=parseInt(document.calculator.x.value);

y=parseInt(document.calculator.y.value);

c=document.getElementById("c");

c1=document.getElementById("c1");

sp=document.getElementById("sp");

c2=document.getElementById("c2");

c3=document.getElementById("c3");

if (c2.checked==true) 

{

z=x*y;

//alert(z);

sp.innerHTML=z;

c.checked=false;

c1.checked=false;

c3.checked=false;

}

}

function division()

{

var x,y,c,c1,z,sp,c2,c3;

x=parseInt(document.calculator.x.value);

y=parseInt(document.calculator.y.value);

c=document.getElementById("c");

c1=document.getElementById("c1");

sp=document.getElementById("sp");

c2=document.getElementById("c2");

c3=document.getElementById("c3");

if (c3.checked==true) 

{

z=x/y;

//alert(z);

sp.innerHTML=z;

c.checked=false;

c1.checked=false;

c2.checked=false;

}

}

</script>

</head>

<body>

<form name="calculator">

Enter first Number<input type="text" name="x"><br>

Enter second number<input type="text" name="y"><br>

Answer:<span id="sp"></span>

<input type="checkbox" name="c" id="c" value="add" onclick="Addition();">Add

<input type="checkbox" name="c1" id="c1" value="sub" onclick="Substraction();">Sub

<input type="checkbox" name="c2" id="c2" value="multiplication" onclick="multiplication();">multi

<input type="checkbox" name="c3" id="c3" value="division" onclick="division();">div


</body>

</html>


Problem 2. 

Cyclic Sort (easy)

Problem Statement

We are given an array containing ‘n’ objects. Each object, when created, was assigned a unique number from 1 to ‘n’ based on their creation sequence. This means that the object with sequence number ‘3’ was created just before the object with sequence number ‘4’. (Concept)

Javascript Code :

function cyclic_sort(nums) {

  let i = 0;

  while (i < nums.length) {

    const j = nums[i] - 1;

    if (nums[i] !== nums[j]) {

      [nums[i], nums[j]] = [nums[j], nums[i]]; // swap

    } else {

      i += 1;

    }

  }

  return nums;

}


console.log(cyclic_sort([3, 1, 5, 4, 2]));

console.log(cyclic_sort([2, 6, 4, 3, 1, 5]));

console.log(cyclic_sort([1, 5, 6, 4, 3, 2]));

 


Post a Comment

0 Comments