0% found this document useful (0 votes)
115 views2 pages

JavaScript Basics: Loops and Functions

The document contains two JavaScript programs. The first program uses a for loop to repeat the text "I am part of loop" 10 times. The second program (a) prompts the user for three integers and displays the largest, and (b) multiplies each input by 5 and displays the results.

Uploaded by

saw monmyatoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views2 pages

JavaScript Basics: Loops and Functions

The document contains two JavaScript programs. The first program uses a for loop to repeat the text "I am part of loop" 10 times. The second program (a) prompts the user for three integers and displays the largest, and (b) multiplies each input by 5 and displays the results.

Uploaded by

saw monmyatoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Create JavaScript program for the following and using appropriate variables,
JavaScript inbuilt functions and control structures.
The page starts with a note, ”Get ready for some repeated text ” . And shows
how this will appear “I am part of a loop” is repeated ten times. The page ends
with a note, “Now, we are back to the plain HTML code.”
<html>
<head>
<title> For looping</title>
</head>
<body>
<script language="JavaScript">
[Link]("Get ready for some repeated text.<br>");
var count;
for (count=1; count<11; count+=1)
{
[Link]("I am part of loop!<br>");
}
[Link]("Now we are back to the plain HTML code");
</script>
</hmtl>
2. Create JavaScript program for the following and using appropriate variables,
JavaScript inbuilt functions and control structures.
(a) To accept three integers and display largest number of them.
(b) Calculate and display the results by multiplying each input value with 5.
<HTML>
<HEAD>
<TITLE> External .js File- Page 1</TITLE>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
var input1 = [Link]( "Enter first number", "0" );
var input2 = [Link]( "Enter second number", "0" );
var input3 = [Link]( "Enter third number", "0" );
var value1 = parseFloat( input1 );
var value2 = parseFloat( input2 );
var value3 = parseFloat( input3 );
varmaxValue = manimum( value1, value2, value3 );
[Link]( "First number: " + value1 +

"<BR>Second number: " + value2 +


"<BR>Third number: " + value3 +
"<BR>Manimum is: " + minValue );
function manimum( x, y, z ) {
return [Link]( x, [Link]( y, z ) );
}
[Link]("<br>Smallest number is" + minValue);
var output1=value1 * 5;
var output2=value2 * 5;
var output3=value * 5;
[Link]("<br> result is:" + output1);
</SCRIPT>
</BODY>
</HTML>

Common questions

Powered by AI

The use of `parseFloat` is crucial for converting the input received from `window.prompt`, which is initially in string format, into a floating-point number that can be used for arithmetic operations. This conversion ensures that the inputs can be accurately compared mathematically to determine the largest or smallest number and for subsequent calculations that involve multiplication by 5. Without this conversion, operations may yield incorrect results or errors due to type incompatibility .

Logical and syntactical errors are present in the script that calculates the largest of three numbers. Notably, the script erroneously defines a function 'manimum' that suggests calculation of a minimum, not a maximum, of the values. Furthermore, there is a typo in 'minValue' which contradicts the goal of identifying the maximum. The result is a misalignment between the function name, its purpose, and the presented result, affecting the program’s accuracy in displaying the largest number. Correcting these would require renaming the function to 'maximum' and adjusting logic to use `Math.max` instead .

Using `document.write` can lead to potential XSS (cross-site scripting) vulnerabilities where user input may introduce malicious script content. This risk is increased when content source is dynamically generated from user inputs without proper sanitization. Mitigating these risks involves avoiding `document.write` for user-generated content and instead using safer methods like `textContent` or `innerHTML` with careful input validation and sanitation. Implementing Content Security Policies (CSPs) further strengthens security by restricting allowed content sources .

The program uses a for loop structure that initializes a counter variable 'count' at 1, checks the condition 'count<11' at each iteration, and increments the counter by 1 in each loop cycle (`count+=1`). This setup ensures that the block of code within the loop is executed ten times precisely. It demonstrates the concept of loop control in terms of initialization, condition-checking, and iteration step, enabling precise control over how and when the loop executes .

The JavaScript program uses a for loop as the control structure to repeat the text output within the web page. The loop initializes a variable 'count' and iterates from 1 to 10, inclusive. In each iteration, the `document.write` function outputs the string 'I am part of loop!' followed by a line break, effectively repeating the text ten times. This demonstrates an effective use of a loop control structure to manipulate HTML output on the page .

To enhance usability, strategies include better naming conventions to reflect variable purposes, fixing syntax errors, and organizing code with whitespace and comments for readability. Employing functions for repeated tasks and using modern JavaScript ES6+ features, like template literals, can make the code more maintainable. Additionally, separating concerns by moving script logic to an external .js file improves clarity and reuse while reducing in-line script clutter within the HTML .

Several issues in the program relate to variable names and function implementation. First, the function 'manimum' has a misspelling and does not align with its purpose. It should be named 'minimum' to reflect that it calculates the smallest number. Second, 'varmaxValue' should be corrected to 'var maxValue' to define a variable properly. Additionally, there is an error in 'var output3=value * 5;' where 'value' should be 'value3'. Correcting these errors ensures the function calculates and outputs correctly .

The integration of HTML and JavaScript allows dynamic content generation on web pages, illustrated by using `document.write` within script tags to manipulate HTML output directly. This combination offers added interactivity and engagement, enabling updates to the page without server requests. It supports a more responsive user experience by automating content generation and handling user inputs efficiently through embedded script logic within the HTML structure .

Enhancing user interaction could involve adding error handling for invalid inputs and providing user guidance or feedback when inputs are incorrect. Implementing input validation checks could alert the user if non-numeric values are entered, improving the robustness of the code. Additionally, introducing labelled buttons for each operation and displaying results dynamically, instead of using `document.write`, could provide a more interactive and responsive experience. Such modifications could make the interaction more intuitive and prevent user frustration from input errors .

Typos in variable names can lead to undefined variables, causing errors during execution or logical inaccuracies. For instance, 'varmaxValue' would be treated as a concatenation of 'var' and 'maxValue', rather than a single variable declaration. Similarly, using 'value' instead of 'value3' in 'var output3=value * 5;' results in unintended arithmetic operations, potentially with undefined variables, which would break the program’s functionality. Such issues highlight the importance of consistent naming and emphasize thorough testing and debugging practices .

You might also like