Advertisement
ARayofLight

Javascript Prac Code

Nov 15th, 2023 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------AREA---------------------------------
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.     <title>Find the area</title>
  6. </head>
  7. <body>
  8.     <h1>Area of triangle using Javascript</h1>
  9.     <label>Enter length of Side 1 of triangle =</label>
  10.     <input type="number" id="side1"><br><br>
  11.     <label>Enter length of Side 2 of triangle -</label>
  12.     <input type="number" id="side2"><br><br>
  13.     <label>Enter length of Side 3 of triangle </label>
  14.     <input type="number" id="side3"><br><br>
  15.     <button type="button" id="areatri">Submit</button>
  16.     <p>AREA OF TRIANGLE: <span id="display"></span></p>
  17.     <script>
  18.         document.getElementById("areatri").onclick = function() {
  19.             var side1 = parseInt(document.getElementById("side1").value);
  20.             var side2 = parseInt(document.getElementById("side2").value);
  21.             var side3 = parseInt(document.getElementById("side3").value);
  22.             var s = (side1 + side2 + side3) / 2;
  23.             var area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)).toFixed(2);
  24.             document.getElementById("display").innerHTML = area;
  25.         }
  26.     </script>
  27.     <h1>Area of Rectangle using Javascript</h1>
  28.     <label>Enter length of rectangle -</label>
  29.     <input type="number" id="len"><br><br>
  30.     <label>Enter breadth of rectangle</label>
  31.     <input type="number" id="bre"><br><br>
  32.     <button type="button" id="arearec">Submit</button>
  33.     <p>AREA OF RECTANGLE: <span id="display1"></span></p>
  34.  
  35.     <script>
  36.         document.getElementById("arearec").onclick = function() {
  37.             var len = parseInt(document.getElementById("len").value);
  38.             var bre = parseInt(document.getElementById("bre").value);
  39.             var area = (len * bre).toFixed(2);
  40.             document.getElementById("display1").innerHTML = area;
  41.         }
  42.     </script>
  43.  
  44.     <h1>Area of Circle using JavaScript</h1>
  45.  
  46.     <label>Enter radius of Circle =</label>
  47.     <input type="number" id="rad"><br><br>
  48.  
  49.     <button type="button" id="areacir">Submit</button>
  50.     <p>AREA OF CIRCLE: <span id="display2"></span></p>
  51.  
  52.     <script>
  53.         document.getElementById("areacir").onclick = function() {
  54.             var rad = parseInt(document.getElementById("rad").value);
  55.             var area = (3.14 * rad * rad).toFixed(2);
  56.             document.getElementById("display2").innerHTML = area;
  57.         }
  58.     </script>
  59. </body>
  60. </html>
  61.  
  62.  
  63.  
  64.  
  65. --------------------------MULTIPLICATION TABLE-------------------------
  66. <!DOCTYPE html>
  67. <html>
  68. <head>
  69.     <title>Multiplication table of 2</title>
  70. </head>
  71. <body>
  72.     <h1>Multiplication table of 2</h1>
  73.  
  74.     <script>
  75.         // Define the number for which you want to create the multiplication table
  76.         const number = 2;
  77.  
  78.         // Create a multiplication table for the number
  79.         for (let i = 1; i <= 10; i++) {
  80.             // Multiply i with the number
  81.             const result = i * number;
  82.  
  83.             // Display the result in the console
  84.             console.log(number + " * " + i + " = " + result);
  85.  
  86.             // Display the result on the web page
  87.             document.write("<p>" + number + " * " + i + " = " + result + "</p>");
  88.         }
  89.     </script>
  90. </body>
  91. </html>
  92.  
  93.  
  94.  
  95.  
  96. ---------------------------OPERATIONS ON STRINGS-----------------------------
  97. <!DOCTYPE html>
  98. <html>
  99.  
  100. <body>
  101.     <h2>String</h2>
  102.     <button onclick="RevStr()">Reverse a String</button>
  103.     <button onclick="Rep1Chr()">Replace Characters</button>
  104.     <p id="test"></p>
  105.     <p id="demo">My Music with Microsoft Apps</p>
  106.     <script>
  107.         function Rep1Chr() {
  108.             document.getElementById("test").innerHTML = "Replace all occurrences of M with W in the paragraph: My Music with Microsoft Apps";
  109.             let text = document.getElementById("demo").innerHTML;
  110.             document.getElementById("demo").innerHTML = text.replace(/M/g, "W");
  111.         }
  112.  
  113.         function RevStr() {
  114.             // Empty string
  115.             let revString = "";
  116.             let str = prompt("Enter a String:");
  117.  
  118.             for (let i = str.length - 1; i >= 0; i--) {
  119.                 revString += str[i];
  120.             }
  121.  
  122.             console.log("Given String: " + str + "<br>Reversed String: " + revString);
  123.             PalStr(str, revString);
  124.         }
  125.  
  126.         function PalStr(str, revString) {
  127.             // Compare the original and reversed strings
  128.             if (str === revString) {
  129.                 console.log('It is a palindrome');
  130.             } else {
  131.                 console.log('It is not a palindrome');
  132.             }
  133.         }
  134.     </script>
  135. </body>
  136.  
  137. </html>
  138.  
  139.  
  140.  
  141.  
  142.  
  143. ---------------------------COMPARE STRINGS--------------------------
  144. <!DOCTYPE html>
  145. <html>
  146.  
  147. <body>
  148.     <h3>Compare Strings</h3>
  149.     Enter String 1: <input id="string1"><br>
  150.     Enter String 2: <input id="string2"><br>
  151.     <button onclick="comp1(); comp2(); comp3();">Compare</button>
  152.  
  153.     <script>
  154.         function comp1() {
  155.             // Using toUpperCase()
  156.             var string1 = document.getElementById("string1").value;
  157.             var string2 = document.getElementById("string2").value;
  158.  
  159.             // Compare both strings
  160.             const result = string1.toUpperCase() === string2.toUpperCase();
  161.             console.log("Using toUpperCase()");
  162.  
  163.             if (result) {
  164.                 console.log('The strings are similar.');
  165.             } else {
  166.                 console.log('The strings are not similar.');
  167.             }
  168.         }
  169.  
  170.         function comp2() {
  171.             // Using RegEx
  172.             var string1 = document.getElementById('string1').value;
  173.             var string2 = document.getElementById("string2").value;
  174.  
  175.             // Create a regex pattern
  176.             const pattern = new RegExp(string1, "gi");
  177.  
  178.             // Compare the strings
  179.             const result = pattern.test(string2);
  180.             console.log("Using RegEx");
  181.  
  182.             if (result) {
  183.                 console.log('The strings are similar.');
  184.             } else {
  185.                 console.log('The strings are not similar.');
  186.             }
  187.         }
  188.  
  189.         function comp3() {
  190.             // Using localeCompare()
  191.             var string1 = document.getElementById("string1").value;
  192.             var string2 = document.getElementById("string2").value;
  193.  
  194.             const result = string1.localeCompare(string2, undefined, {
  195.                 sensitivity: 'base'
  196.             });
  197.  
  198.             console.log("Using localeCompare()");
  199.  
  200.             if (result === 0) {
  201.                 console.log('The strings are similar.');
  202.             } else {
  203.                 console.log('The strings are not similar.');
  204.             }
  205.         }
  206.     </script>
  207. </body>
  208.  
  209. </html>
  210.  
  211.  
  212.  
  213.  
  214.  
  215. -----------------------------TIMER----------------------------
  216. <!DOCTYPE html>
  217. <html>
  218.  
  219. <body>
  220.     <h3>Building Countdown Timer Using Date Function</h3>
  221.     <p id="demo"></p>
  222.     <script>
  223.         // Set the date we're counting down to
  224.         var tDate = new Date();
  225.         document.write("<br>" + "Today's Date is: " + tDate);
  226.         document.write("<br>" + "Month is: " + tDate.getMonth());
  227.         document.write("<br>" + "Day is: " + tDate.getDate());
  228.         document.write("<br>" + "Time is: " + tDate.getTime() + " msec");
  229.         var tt = tDate.getTime();
  230.         var countDownDate = new Date("Jan 5, 2024 15:37:25").getTime();
  231.         var diff = countDownDate - tt;
  232.         document.write('<br>' + "Time in msec from 1 Jan 1970 till Jan 5, 2024 is: " + countDownDate);
  233.         document.write("<br>" + "Difference: " + diff);
  234.  
  235.         // Update the countdown every 1 second
  236.         var x = setInterval(myTimer, 1000);
  237.  
  238.         function myTimer() {
  239.             // Get today's date and time
  240.             var now = new Date().getTime();
  241.  
  242.             // Find the distance between now and the countdown date
  243.             var distance = countDownDate - now;
  244.  
  245.             // Time calculations for days, hours, minutes, and seconds
  246.             var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  247.             var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  248.             var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  249.             var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  250.  
  251.             // Display the result in the element with id="demo"
  252.             document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
  253.  
  254.             // If the countdown is finished, write some text
  255.             if (distance < 0) {
  256.                 clearInterval(x);
  257.                 document.getElementById("demo").innerHTML = "EXPIRED";
  258.             }
  259.         }
  260.     </script>
  261. </body>
  262.  
  263. </html>
  264.  
  265.  
  266.  
  267.  
  268. --------------------------ARRAY OPERATIONS-----------------------------
  269.  
  270. <!DOCTYPE html>
  271. <html>
  272. <body>
  273. <h3>Demonstrate Array Operations</h3>
  274. <button onclick="removeElement()">Remove Element</button>
  275. <button onclick="chkValue()">Check Value</button>
  276. <button onclick="emptyArray()">Empty Array</button>
  277.  
  278. <script>
  279. function removeElement() {
  280.     var shoeBrand = ["Nike", "Adidas", "Puma", "Woodland"];
  281.     console.log("Elements in array before removing: " + shoeBrand);
  282.  
  283.     // Removing the last element from the array
  284.     var poppedElement = shoeBrand.pop();
  285.     console.log("Removed last element from array using pop(): " + poppedElement);
  286.  
  287.     // Display the remaining elements present in the array after removing
  288.     console.log("New array: " + shoeBrand);
  289.  
  290.     // Removing 2 elements from position "1"
  291.     shoeBrand.splice(1, 2);
  292.     console.log("Removed elements from array using splice(1, 2):\nNew Array: " + shoeBrand);
  293. }
  294.  
  295. function chkValue() {
  296.     var carBrand = ["Mercedes", "BMW", "Jeep", "Porsche"];
  297.     console.log(carBrand);
  298.  
  299.     var str = prompt("Enter carBrand Value");
  300.     const hasValue = carBrand.includes(str);
  301.     console.log(carBrand);
  302.     console.log("Value entered: " + str);
  303.  
  304.     // Check the condition
  305.     if (hasValue) {
  306.         console.log('Array contains: ' + str);
  307.     } else {
  308.         console.log("Array does not contain: " + str);
  309.     }
  310. }
  311.  
  312. function emptyArray() {
  313.     var carBrand = ["Mercedes", "BMW", "Jeep", "Porsche"];
  314.     console.log(carBrand);
  315.     carBrand.splice(0, carBrand.length);
  316.     console.log("Empty Array: " + carBrand);
  317. }
  318. </script>
  319. </body>
  320. </html>
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. ---------------------------SET OPERATIONS------------------------------
  328. <!DOCTYPE html>
  329. <html>
  330. <head>
  331. <h2>Demonstrate Set Operations</h2>
  332. <p>Set A: {'apple', 'mango', 'orange'}</p>
  333. <p>Set B: {'grapes', 'apple', 'banana'}</p>
  334. <p>Set C: {'apple', 'orange'}</p>
  335. <button onclick="union()">Union of Sets</button>
  336. <button onclick="intersection()">Intersection of Sets</button>
  337. <button onclick="difference()">Difference of Sets</button>
  338. <button onclick="subset()">Subset of Set</button>
  339. </head>
  340. <body>
  341. <script>
  342. // Two sets of fruits
  343. const setA = new Set(['apple', 'mango', 'orange']);
  344. const setB = new Set(['grapes', 'apple', 'banana']);
  345. const setC = new Set(['apple', 'orange']);
  346.  
  347. function union() {
  348.     let unionSet = new Set(setA);
  349.     for (let i of setB) {
  350.         unionSet.add(i);
  351.     }
  352.     console.log(unionSet);
  353. }
  354.  
  355. function intersection() {
  356.     let intersectionSet = new Set();
  357.     for (let i of setB) {
  358.         if (setA.has(i)) {
  359.             intersectionSet.add(i);
  360.         }
  361.     }
  362.    console.log(intersectionSet);
  363. }
  364.  
  365. function difference() {
  366.     let differenceSet = new Set(setA);
  367.     for (let i of setB) {
  368.         differenceSet.delete(i);
  369.     }
  370.     console.log(differenceSet);
  371. }
  372.  
  373. function subset() {
  374.     for (let i of setC) {
  375.         if (!setA.has(i)) {
  376.             console.log(false);
  377.             return;
  378.         }}
  379.     console.log(true);
  380. }
  381. </script>
  382. </body>
  383. </html>
  384.  
  385.  
  386.  
  387.  
  388.  
  389. ----------------------------HOMEPAGE AND BG COLOR------------------------
  390. <!DOCTYPE html>
  391. <html>
  392. <body>
  393. <h2>Demonstrate mouseover() and focusFunction()</h2>
  394. <h3 id="demo" onmouseover="mouseover()" onmouseout="mouseout()">Mouse over me</h3>
  395. <input type="text" placeholder="Enter Text" id="focus" onfocus="focusFunction()" onblur="blurFunction()">
  396. <script>
  397. function mouseover() {
  398.     document.getElementById("demo").style.color = "red";
  399. }
  400.  
  401. function mouseout() {
  402.     document.getElementById("demo").style.color = "black";
  403. }
  404.  
  405. function focusFunction() {
  406.     document.getElementById("focus").style.background = "white";
  407. }
  408.  
  409. function blurFunction() {
  410.     document.getElementById("focus").style.background = "red";
  411. }
  412. </script>
  413. </body>
  414. </html>
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421. ------------------------------CALCULATOR-------------------------
  422.  
  423. <!DOCTYPE html>
  424. <html>
  425. <body>
  426. <input id="text1" placeholder="Enter Number">
  427. <input id="text2" placeholder="Enter Number">
  428. <br>
  429. <button onclick="sum()" id="btn">Add</button>
  430. <button onclick="diff()" id="btn">Subtract</button>
  431. <button onclick="mult()" id="btn">Multiply</button>
  432. <button onclick="div()" id="btn">Divide</button>
  433. <input id="result" placeholder="Result">
  434.  
  435. <script>
  436. function sum() {
  437.     var x = parseFloat(document.getElementById("text1").value);
  438.     var y = parseFloat(document.getElementById("text2").value);
  439.     var result = x + y;
  440.     document.getElementById("result").value = "Sum: " + result;
  441. }
  442.  
  443. function diff() {
  444.     var x = parseFloat(document.getElementById("text1").value);
  445.     var y = parseFloat(document.getElementById("text2").value);
  446.     var result = x - y;
  447.     document.getElementById("result").value = "Difference: " + result;
  448. }
  449.  
  450. function mult() {
  451.     var x = parseFloat(document.getElementById("text1").value);
  452.     var y = parseFloat(document.getElementById("text2").value);
  453.     var result = x * y;
  454.     document.getElementById("result").value = "Product: " + result;
  455. }
  456.  
  457. function div() {
  458.     var x = parseFloat(document.getElementById("text1").value);
  459.     var y = parseFloat(document.getElementById("text2").value);
  460.     var result = x / y;
  461.     document.getElementById("result").value = "Quotient: " + result;
  462. }
  463. </script>
  464. </body>
  465. </html>
  466.  
  467.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement