0% found this document useful (0 votes)
3 views

CSS Practical No 09

The document contains two practical programming tasks in HTML and JavaScript. The first task demonstrates string to number conversion using functions like parseInt, Number, and parseFloat. The second task showcases various intrinsic functions including string manipulation, mathematical operations, type conversions, and array methods.

Uploaded by

faizanmulla1202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSS Practical No 09

The document contains two practical programming tasks in HTML and JavaScript. The first task demonstrates string to number conversion using functions like parseInt, Number, and parseFloat. The second task showcases various intrinsic functions including string manipulation, mathematical operations, type conversions, and array methods.

Uploaded by

faizanmulla1202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No 09

Q.1) Write a program to convert the string to number using


different functions ( parseIntnumber parsefloat).
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String to Number Conversion</title>
</head>
<body>
<h2>String to Number Conversion</h2>

<input type="text" id="inputString" placeholder="Enter a string to convert">


<button onclick="convertString()">Convert</button>

<div id="output"></div>

<script>
function convertString() {
const inputString = document.getElementById('inputString').value;
let output = '';

// Using parseInt()
const intResult = parseInt(inputString);
output += `Using parseInt: ${intResult} (Type: ${typeof intResult})<br>`;

// Using Number()
const numberResult = Number(inputString);
output += `Using Number(): ${numberResult} (Type: ${typeof numberResult})<br>`;

// Using parseFloat()
const floatResult = parseFloat(inputString);
output += `Using parseFloat: ${floatResult} (Type: ${typeof floatResult})<br>`;

// Display the output


document.getElementById('output').innerHTML = output;
}
</script>
</body>
</html>

Output:
Q.2) Program to perform all intrinsic functions.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Intrinsic Functions</title>
</head>
<body>
<h2>Intrinsic Functions Example</h2>

<button onclick="performOperations()">Perform Operations</button>

<div id="output"></div>

<script>
function performOperations() {
const str = " Faizan ";
const num = 123.456;
const arr = [1, 2, 3, 4, 5];

let output = '';

// String functions
output += `<h3>String Functions:</h3>`;
output += `Trimmed String: '${str.trim()}'<br>`;
output += `Uppercase: '${str.toUpperCase()}'<br>`;
output += `Substring (0, 5): '${str.substring(0, 5)}'<br>`;
output += `Replaced 'World' with 'JavaScript': '${str.replace('World', 'JavaScript')}'<br>`;
output += `Split by ', ': ${str.split(', ')}<br>`;

// Mathematical functions
output += `<h3>Mathematical Functions:</h3>`;
output += `Rounded Number: ${Math.round(num)}<br>`;
output += `Ceiling of Number: ${Math.ceil(num)}<br>`;
output += `Floor of Number: ${Math.floor(num)}<br>`;
output += `Square Root: ${Math.sqrt(num)}<br>`;
output += `Random Number between 0 and 1: ${Math.random()}<br>`;

// Type conversion functions


output += `<h3>Type Conversion Functions:</h3>`;
output += `String to Number (parseInt): ${parseInt(num)}<br>`;
output += `String to Number (parseFloat): ${parseFloat(num)}<br>`;
output += `Number to String: '${num.toString()}'<br>`;

// Array functions
output += `<h3>Array Functions:</h3>`;
output += `Array Length: ${arr.length}<br>`;
output += `Array to String: '${arr.toString()}'<br>`;
output += `Array with 10 added: ${arr.map(x => x + 10)}<br>`;
output += `Array Join: '${arr.join(', ')}'<br>`;
output += `Array Reverse: ${arr.reverse().toString()}<br>`;

// Display the output


document.getElementById('output').innerHTML = output;
}
</script>
</body>
</html>
Output:

You might also like