0% found this document useful (0 votes)
2 views8 pages

3 TT284 HTML Combined With JavaScript

The document contains multiple JavaScript code snippets embedded in HTML that perform various tasks such as solving equations, counting positive and negative numbers, calculating factorials and averages, converting currencies, and determining grades based on input. Each section includes a prompt for user input and displays the result using document.write. The code examples illustrate fundamental programming concepts like loops, functions, and conditional statements.

Uploaded by

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

3 TT284 HTML Combined With JavaScript

The document contains multiple JavaScript code snippets embedded in HTML that perform various tasks such as solving equations, counting positive and negative numbers, calculating factorials and averages, converting currencies, and determining grades based on input. Each section includes a prompt for user input and displays the result using document.write. The code examples illustrate fundamental programming concepts like loops, functions, and conditional statements.

Uploaded by

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

TT284 HTML combined with JavaScript

Write JavaScript code that will solve the following equation:

𝑥×5 𝑛
𝑧= +
𝑦 2

<html>
<body>

1. <script>

x = Number(prompt("Enter x: "));
y = Number(prompt("Enter y: "));
n = Number(prompt("Enter n: "));
z = ((x* 5) /y) + (n/2);
[Link]("Z = ", z);

</script>

</body>
</html>

Write JavaScript code that will ask the user to input two numbers, then print the
numbers starting from the first number ending with the second number.

<html>
<body>

<script>
2.
n1 = Number(prompt("Enter n1: "));
n2 = Number(prompt("Enter n2"));
for (i = n1; i<= n2; i++)
[Link]("<br> " + i );

</script>

</body>
</html>

1
TT284 HTML combined with JavaScript
Write JavaScript code that will ask the user to input five numbers, then print the
number of positive numbers, negative numbers and zeros entered by the user.

<html>
<body>
<script>
positive = 0;
negative = 0;
zero = 0;
for (i = 1; i<= 5; i++)
{
n = Number(prompt("Enter n: "));
if (n > 0)
3.
positive = positive + 1;
else if (n < 0)
negative++;
else
zero++;
}
[Link]("<br> Positive = " + positive);
[Link]("<br> Negative = " + negative);
[Link]("<br> zero = " + zero );

</script>
</body>
</html>
Write JavaScript code that will ask the user to input five numbers, then print the
maximum number.

<html>
<body>
<script>
n = Number(prompt("Enter n: "));
max = n ;
4.
for (i = 1; i<= 4; i++)
{
n = Number(prompt("Enter n: "));
if (n > max)
max = n;
}
[Link]("<br> Max = " + max);
</script>
</body>
</html>

2
TT284 HTML combined with JavaScript
Write JavaScript code that will ask the user to input a number, then print the factorial
number of the entered number.

<html>
<body>

<script>

n = Number(prompt("Enter n: "));
5. fact = 1;
for (i = 1; i<= n; i++)
{
fact = fact * i;
}
[Link]("<br> Factorial = " + fact);

</script>

</body>
</html>

Write JavaScript code that will ask the user to input five numbers, then calculate and
print the average of the numbers.

<html>
<body>

<script>

sum = 0;
avg = 0;
6. for (i = 1; i<= 5; i++)
{
n = Number(prompt("Enter n: "));
sum = sum + n;
}
avg = sum / 5;
[Link]("<br> Average = " + avg);

</script>

</body>
</html>

3
TT284 HTML combined with JavaScript
Write HTML code included with JavaScript code. The combined codes will ask the user
to input the amount wanted to be converted from Dollar to Riyal, then call a function
that will perform the conversion and print the result.

Note:
1 Dollar = 3.75 Riyal.
<html>
<body>
<script>
function dollartoriyal(dol)
7. {
riyal= dol * 3.75;
[Link]("Number of riyals = " , riyal);
}
dollar = prompt("Enter number of dollars : ");
dollartoriyal(dollar);

</script>

</body>
</html>
Write HTML code included with JavaScript code. The combined codes will ask the user
to input the base and the height values, then call a function that will perform the
calculation of the area of the triangle.

Note:
1
Area of triangle = 2 (𝑏𝑎𝑠𝑒)(h𝑒𝑖𝑔h𝑡)

<html>
<body>
8. <script>
function areaoftriangle(b,h)
{
area = 0.5 * b * h;
[Link]("Area of triangle = " , area ) ;
}
base = Number(prompt("Enter base : "));
height = Number(prompt("Enter height: "));
areaoftriangle( base,height);
</script>

</body>
</html>

4
TT284 HTML combined with JavaScript
Write HTML code included with JavaScript code. The combined codes will ask the user
to input two numbers, then call a function that will find and print the maximum number
of the two entered numbers or they are equal.

<html>
<body>
<script>
function greater(n1 , n2)
{
if (n1 > n2)
[Link](n1 + " is greater than " + n2) ;
else
9.
if (n1 < n2)
[Link](n2 + " is greater than " + n1) ;
else
[Link](" The two numbers are equal") ;
}
num1 = Number(prompt("Enter num1: "));
num2 = Number(prompt("Enter num2: "));

greater(num1,num2);
</script>

</body>
</html>
Write HTML code included with JavaScript code. The combined codes will ask the user
to input a number, then call a function that will find and print the sum of numbers from 1
to the number entered by the user.

<html>
<body>
<script>

function range(n)
{
10. sum =0;
for (i=1; i<=n; i++)
sum = sum + i;
[Link]("<br> Sum = " + sum) ;
}
number = Number(prompt("Enter number: "));
range(number);

</script>

</body>
</html>

5
TT284 HTML combined with JavaScript
Write HTML code included with JavaScript code. The combined codes will ask the user
to input the grade of student, then it will call a function to find the grade letter that is
corresponding to the grade of the student based on the following criteria:

• Student gets A if the grade is >= 90


• Student gets B if the grade is < 90 and >=80
• Student gets C if the grade is < 80 and >=70
• Student gets D if the grade is < 70 and >=60
• Student gets F if the grade is < 60

<html>
<body>

<script>

function gradeletter(g)
{
if (g >= 90 && g <= 100 )
{
[Link]("You got A in the course");
11.
}
else
if (g >= 80 && g <90 )
[Link]("You got B in the course");
else
if (g >= 70 && g<80 )
[Link]("You got C in the course");
else
if (g >= 60 && g < 70 )
[Link]("You got D in the course");
else
[Link]("You got F in the course") ;
}
grade= Number(prompt("Enter grade: "));
gradeletter(grade);

</script>

</body>
</html>

6
TT284 HTML combined with JavaScript
Write HTML code included with JavaScript code. The combined codes will ask the user
to input number, then call a function that will check that the entered number is divisible
by both 2 and 4 or not, then display a message showing the result of the check.

<html>
<body>
<script>
function divisible(n)
{
if (n % 2 == 0 && n % 4 == 0 )
{
12.
[Link](n + " is divisible by 2 and 4");
}
else
{
[Link](n + " is not divisible by 2 and 4");
}
}
number = Number(prompt("Enter number: "));
divisible(number);
</script>
</body>
</html>
Write HTML code included with JavaScript code. The combined codes will ask the user
to input two numbers, then call a function that will check that the first entered number is
a multiple of the second one or not, then display a message showing the result of the
check.
<html>
<body>
<script>
function multible(n1,n2)
{
if (n1 % n2 == 0)
{
13. [Link](n1 + " is a multiple of " + n2);
}
else
{
[Link](n1 + " is not a multiple of " + n2);
}
}
number1 = Number(prompt("Enter number1: "));
number2 = Number(prompt("Enter number2: "));
multible(number1,number2);
</script>
</body>
</html>

7
TT284 HTML combined with JavaScript
What is the exact output of the following code?
<html>
<body>
<script>
function perform(n1,n2)
{
value = 0
for (i=n1; i<=n2; i++)
{
value = value + i;
14. [Link]("<br> " + value) ;
}
}
perform(4,8);
</script>
</body>
</html>
4
9
15
22
30

You might also like