Javascript Tutorials and Programs

By: Prof. Fazal Rehman Shamil | Whatsapp:+923028700085

Javascript Tutorials and Programs

Javascript Tutorials and Programs are the topics of discussion for today tutorial.

Addition, Subtraction, Multiplication, and Division of two numbers

Algorithm of the sum of two numbers

Step 1:  Start

Step 2:  Declare variables number1, number2 and sum.

Step 3:   Input values number1 and number2.

Step 4:   Add number1 and number2 and assign the result to sum.

Sum = number 1 + number2

Step 5:   Display sum

Pseudocode of the sum of two numbers

Begin

Entering number1

Entering number2

Compute sum as number1 and number2

Display   sum of given two numbers in the sum

End

Program of two numbers

Program of Multiplying two numbers

Program of Division of two numbers

Program of Subtraction of two numbers

Addition of Three Numbers

Algorithm of Addition of Three Numbers in Javascript

  • Start
  • First of all, we open the specific programming software to solve the program.
  • Then open the new file and save file name of your choice.
  • Start program with HTML open syntax
  • Then JavaScript open syntax.
  • Declare the function name in this program I declare (Calculate) function name.
  • Then declare the variables name.
  • Then the addition of values assigns and stores the equal numbers in n4.
  • The value of button assign through which data is displayed.
  • After that close the script syntax.
  • Close the head syntax.
  • Open the body syntax.
  • Open the form syntax and assign the name of a form of your choice.
  • I assign the (frm) name to form.
  • Now we move to the front end of the program.
  • Using HTML we use input data type in which user enter the values to sum.
  • Using three input data type.
  • We use the button through which data is added and display the sum of the numbers in button.

Addition of Three Numbers in Javascript

Output

The greatest number among 3 numbers in Javascript

Algorithm of greatest number among 3 numbers

Step1: Start.

Step2: Declare three variables x,y,z.

Step3: Get a number from the user or already give value to these variables.

Step4: If(x>y)

If(x>z)

Display x is the greatest number

Else

Display z is the greatest number

If(y>z)

Display y is the greatest number

Else

Display z is the greatest number

Step 5: End

Program explanation

  • Take the three numbers and store it in the variables x, y, and z respectively.
  • Firstly check if the x is greater than y.
  • If it is, then prints the output as “x is the greatest among three”.\
  • Otherwise, print the output as “z is the greatest among three”.
  • If the x is not greater than y, then check if y is greater than z.
  • If it is, then prints the output as “y is the greatest among three”.
  • Otherwise, print the output as “z is the greatest among three”.
Output

greatest number among 3 numbers Javascript

Algorithm of Number of vowels digits consonant and spaces in the string (Javascript)

Armstrong Number in Javascript

Algorithm of Armstrong Number

START

Step 1 → Take  variable  NO

Step 2 → input some value to variable no

Step 3 → MAKE VARIABLE NAME arm and assign zero to it

Step 4 → create another variable name LAST

Step 5 → TAKE mod of no with 10 and store in variable last

Step 6 →take a cube of last and add it to arm

Step 7 →divide no with 10 and store value to no

Step 8→ If Sum equals to Arms print Armstrong Number

Step 9 → If Sum does not equal to Arms print Not Armstrong Number

STOP

Program of Armstrong Number

Output

Checking Time Difference Program in JS

Algorithm of Checking Time Difference

Step 1: Start.

Step 2: Declare Variables as date1, date2, diff, days, hours, minutes and seconds.

Step 3: Read these Variables date1, date2, diff, days, hours, minutes and seconds.

Step 4: Calculate diff, days, hours, minutes and seconds by using Formulas:

diff ← (date1-date2)/1000

days ← (diff/86400)

hours ← (diff/3600) % 24

minutes ← (diff/60) % 60

seconds ← diff % 60

Step 5: Display Time difference b/w two dates in days, hours, minutes and seconds.

Step 6: Stop.

Pseudo Code of Checking Time Difference

  1. This program checks the time difference b/w two dates in days, hours, minutes and seconds.
  2. Sets input as date1, date2.
  3. Read variables diff, days, hours, minutes and seconds.
  4. Calculate diff of two dates using the formula:
  5. Diff= date1-date2)/1000
  6. Calculate days, hours, minutes and seconds using respective formulas.
  7. Display result/difference.

Program of Checking the Time Difference

Output

time difference in JS

Figure: time difference in JS

Explanation

First, we are declaring two variables date1 and date2.

Now we are assigning or setting values in those two date variables.

After that, we are declaring variables diff, days, hours, minutes and seconds along with their respective formulas.

The formulas will calculate values as the difference in time in days, hours, minutes and seconds.

It’s time to Display the difference in time using output statements.

Length of number in Javascript

Algorithm of Length of a number in Javascript

  • First, make the function to count the length of number and make a button to run behind the button.
  • In the next step, they were looking for reference using id and easily selector the specific tag.
  • In the script call the function using the value with var no1 12666.
  • The document write is used for the expression length.
  • And also the .to string which is used to compare the number.
  • In the next step, they will get the specified value by using attributes.

Pseudo Code of Length of a number in Javascript

The program is used to count the number of value.

Onclick=” the count()”

Make a function to calculate the length of number. That will count or hold the value of number.

<p Id = “demo”></p>

Looking for the id means to easily select the specific tag. And also look for the reference of which is p=id here is the id which contains the reference.

Function the count{

Here is the value which we want to calculate the length of the number. And there is pre value which is stored in the form of a number

Var  no1 = 656554664;

And also number 2 which also contain the value

Var no2 = 145796646;

Document.write (“”) no1.string().lenght

In this value is the value of no1 and is adding and also the to compare the value of the string.

Document.write (“”) no2.string().lenght

In this value is the value of no2  adding and also the to compare the value of the string

Document.getElementById(“demo”).innerHTML = n;

This is the value is specified by using the value of the attribute.

Flowchart of Length of a number in Javascript

Step: 1

Step on the function which is used to count or call.

Step: 2

The function count is used to count the number

Step: 3

Document.Write is used to convert it into a string and pass it to the attributes to show the result

Step: 4

In this step, the number is adding also converting into a string.

Step: 5

In the last step the get value number from to string and recognized by id and show the length of number.

Program of Length of a number in Javascript

Output

Length = 789258693 = 9

Length = 159963478623 = 12

Take three variables and store name, address, and phone number

Algorithm of the program to take three variables and store name, address, and phone number

Step 1:  Start

Step 2:  Declare variables for name address and phone number.

Step 3:   Input values for variables.

Step 4:   show output

Program to take three variables and store name, address, and phone number

Printing the Highest Value in JavaScript

Flowchart of Printing the Highest Value in JavaScript

Javascript Tutorials and Programs

Javascript Tutorials and Programs

Algorithm of printing the Highest Value in JavaScript

  1. Open a new HTML file.
  2. Write Code in the script tag.
  3. Create a function of finding the maximum value.
  4. Use built-in function Math.max to find the maximum value.
  5. Create a button in the HTML body.
  6. Call function on the onclick button.
  7. The output will be displayed in the browser.

Program of printing the Highest Value in JavaScript

String length in javascript

Algorithm of finding the string length in javascript

  • Open HTML, head and body tags.
  • Under head tag, open title tag and set the title of the program and close title tag.
  • Underbody tag open script tag.
  • In script, the tag makes the object of Str.leanth() and write a string that is to be measured.
  • Show output on screen close script tag.
  • Close all other open tags.

Program of finding the string length in javascript

Output

Length of the string is: 37

Area of Rectangle in Javascript

Algorithm of Area of Rectangle in Javascript

  • Take the length of the rectangle.
  • Then take the width of the rectangle.
  • Now multiply length and width of the rectangle that user was given.
  • And the answer will display.

Pseudocode of Area of Rectangle in Javascript

area = width x length.
Display “enter the length of your rectangle:”
Input length.
Display “enter the width of your rectangle:”
Input width.
Display area.

Program of Area of Rectangle in Javascript

Output

Area of the Rectangle

The length of the rectangle is 45.

The width of the rectangle is 45.

The area of the rectangle is 2025.

Even Odd Program in Javascript

Algorithm of Even-Odd Program in Javascript

1)  start

2 ) create function odd-even

3) declare variable n;

4) if the module of variable n with  2 is 0 then show even number

5) else show an odd number

Pseudocode of Even-Odd Program in Javascript

Checking even or odd number

1) Begin

2) declare variable n

3) if n%3=0 print even

4) else print odd

5) end

Program of Even-Odd Program in Javascript

The minimum and maximum value in javascript

Flowchart of the minimum and maximum value

Program of the minimum and maximum value javascript

Figure: Program of the minimum and maximum value javascript

Algorithm of the minimum and maximum value in javascript

  1. Declare an array.
  2. Initialize array.
  3. Compare the first index value with all other values of an array.
  4. Print maximum OR minimum value.

Pseudo code for maximum value

  1. Max = 0;
  2. Declare and initialize array.
  3. Max = a[0];
  4. For loop to array size;
  5. if ( a [ I ] > max ) ;
  6. Print value max variable;

Pseudo code minimum value

  1. Min = 0;
  2. Declare and initialize array.
  3. Min = a [ 0 ];
  4. For loop to array size.
  5. if ( a [ I ] < min)
  6. Print value max OR min variable.

Program of the minimum and maximum value in javascript

Concatenation of Strings in Javascript

Algorithm for Concatenation of String

First, take some string and store this string into any variable.

Take another string and store it in another variable.

Concatenate variables.

PseudoCode for Concatenation of String

  1. String one = any string
  2. String two = any string
  3. String three = string one. Cancat (string two);
  4. Display string three

Program of Concatenation of Strings in Javascript

Program of Concatenation of Strings in Javascript

Figure: Program of Concatenation of Strings in Javascript

Reverse the Number in Javascript

Pseudocode to Reverse the Number in Javascript

  1. Create html tag
  2. Make <button onclick revers function ()
  3. Give id = to “rev”</p>
  4. Start <script>
  5. Array list[1,2,4,5,7,90,34,];
  6. By .innerHTML equal = num;
  7. Get id , to “rev”
  8. Make fun backward();{
  9. Compare No:1.backward();
  10. Get value getelementbyid(“rev”)
  11. }
  12. </script>
  13. Close </body>
  14. </html>

Algorithm of Reverse the Number in Javascript

Create html tag

Make <button onclick revers function ()

Give id = to “rev”</p>

Start <script>

Array list[1,2,4,5,7,90,34,];

By .innerHTML equal = num;

Get id , to “rev”

Make fun backward();{

Compare No:1.backward();

Get value getelementbyid(“rev”)

}

</script>

Close </body>

</html>

Program to Reverse the Number in Javascript

Output

Reverse the Number in Javascript

Figure: Reverse the Number in Javascript

Javascript Program for converting hours into minutes and seconds

Algorithm for converting hours into minutes and seconds

1-start

2-get input from the user,

3-for find minutes n*60.

4-For find seconds n*3600.

5-creat function.

6- call function.

7- stop.

Javascript Program for converting hours into minutes and seconds

Javascript converting hours into minutes and seconds

Swapping Values Program in Javascript

Algorithm of Swapping Values Program 

Algorithm:

Step1:      Start.

Step:2      Read values a and b.

Step3:      a=a+b

b=a-b

a=a-b

Step4:  Print a and b

Step5:   Stop