Programming Lab11

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Programming Lab-2

Name- Deepak Kumar Branch- Data Science


Reg No- 2022DS15 Date – 26/03/2023

Que 1. Write a JavaScript function that accept row, column, (to identify a
particular cell) and a string to update the content of that cell.
File.html
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<script src="sa.js"></script>
<title>Change the content of a cell</title>
<style type="text/css">
body {margin: 30px;}
</style>  
</head><body>
<table id="myTable" border="1">
<tr><td>Row1 cell1</td>
<td>Row1 cell2</td></tr>
<tr><td>Row2 cell1</td>
<td>Row2 cell2</td></tr>
<tr><td>Row3 cell1</td>
<td>Row3 cell2</td></tr>
</table><form>
<input type="button" onclick="changeContent()" value="Change content">
</form></body></html>
Sa.js

function changeContent()
{
rn = window.prompt("Input the Row number(0,1,2)", "0");
cn = window.prompt("Input the Column number(0,1)","0");
content = window.prompt("Input the Cell content");  
var x=document.getElementById('myTable').rows[parseInt(rn,10)].cells;
x[parseInt(cn,10)].innerHTML=content;
}

Input and output:


2.Write a JavaScript function that creates a table, accept row, column numbers
from the user, and input row-column number as content (e.g. Row-0 Column-0) of
a cell.

File.html
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<script src="sa.js"></script>
<title>Create a table</title>
<style type="text/css">
body {margin: 30px;}
</style>  
</head><body>
<table id="myTable" border="1">
</table><form>
<input type="button" onclick="createTable()" value="Create the table">
</form></body></html>

Sa.js
function createTable()
{
rn = window.prompt("Input number of rows", 1);
cn = window.prompt("Input number of columns",1);
 
 for(var r=0;r<parseInt(rn,10);r++)
 {
   var x=document.getElementById('myTable').insertRow(r);
   for(var c=0;c<parseInt(cn,10);c++)  
  {
     var y=  x.insertCell(c);
     y.innerHTML="Row-"+r+" Column-"+c;
  }
   }
}

Input and Output:


3. Write a JavaScript program to count and display the items of a dropdown list, in
an alert window.
File.html
<!DOCTYPE html>
<html><head>
<meta charset=utf-8 />
<script src="sa.js"></script>
<style type="text/css">
  
body {margin: 30px;}
</style>  
<title>Count and display items of a dropdown list - w3resource</title>
</head><body><form>
Select your favorite Game :
<select id="mySelect">
<option>Badminton</option>
<option>Cricket</option>
<option>Tennis</option>
<option>Chess</option>
</select>
<input type="button" onclick="getOptions()" value="Count and Output all items">
</form></body></html>

Js file
Sa.js
function getOptions()
{
var x=document.getElementById("mySelect");
var txt1 = "No. of items : ";
var i;
l=document.getElementById("mySelect").length;  
txt1 = txt1+l;
for (i=0;i<x.length;i++)
 {
    txt1 = txt1 + "\n" + x.options[i].text;
 }
alert(txt1);
}

Input and Output:

4. Write a JavaScript program to count number of words in string Note: Remove


white-space from start and end position. Convert 2 or more spaces to 1. Exclude
newline with a start spacing.

Solution.js
function count_words()
{
str1= "The tree is very tall";
//exclude  start and end white-space
str1 = str1.replace(/(^\s*)|(\s*$)/gi,"");
//convert 2 or more spaces to 1  
str1 = str1.replace(/[ ]{2,}/gi," ");
// exclude newline with a start spacing  
str1 = str1.replace(/\n /,"\n");
 console.log( str1.split(' ').length);
}
console.log(count_words());

Output:

No. 5

Write a JavaScript function:

i. to check whether a given value is a valid url or not.

Solution.js

function check_url(str)
{
reg_exp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!
(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})
(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:
[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\
uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\
u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
if (reg_exp.test(str))
{
console.log("Valid URL"); }
else
{
console.log("Invalid URL");
}
}

console.log(check_url("http://www.google.com"));
console.log(check_url("https://www.mnnit.ac.in"));
console.log(check_url("www.indiatv.in"));
console.log(check_url("www.indiatv.inn.edu.kk"));

Output:
ii. to check whether a given value is alpha numeric or not.

Solution.js

function alphaNumeric(str)
{
 reg_exp = /^[A-Za-z0-9]+$/;
 
        if (reg_exp.test(str))
     {
            console.log("alphanumeric");
     }
        else
     {
            console.log(" not alphanumeric");
     }
}

console.log(alphaNumeric("376ejagduy"));

console.log(alphaNumeric("543254@#jhkjn"));

Output:
iii. to check whether a given value is time string or not.

Solution.js

function timeString(str)
{
 reg_exp = /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$/;
 
        if (reg_exp.test(str))
     {
            console.log("Yes it is a time string");
     }
        else
     {
            console.log("No ,it is not a time string");
     }
}

console.log(timeString("11:35:30"));

console.log(timeString("90:90:90"));

Output:
iv. to check whether a given value is US zip code or not.

Solution.js

function usZipCode(str)
{
 reg_exp = /^[0-9]{5}(?:-[0-9]{4})?$/;
 
        if (reg_exp.test(str))
     {
            console.log("yes a us zip code");
     }
        else
     {
            console.log("not a us zip code");
     }
}

console.log(usZipCode("03201-2150"));
console.log(usZipCode("7892"));

Output:

v. to check a given value contains alpha, dash and underscore.

Solution.js
function alphaDash(str)
{
 regexp = /^[a-z0-9_\-]+$/i;
 
        if (regexp.test(str))
     {
            console.log("yes a alpha dash number");
     }
        else
     {
            console.log("No not a alpha dash number");
     }
}
console.log(alphaDash('12-133'));

console.log(alphaDash('100_23'));

Output:

You might also like