Git Product home page Git Product logo

javascript-exercises-for-beginners's Introduction

Question 1

Question:

Write a program to print Hello World!.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
document.write("Hello World!");
</script>
</body>
</html>

Try it Yourself »

Question 2

Question:

Write a program to add two numbers.


Solution:

<!DOCTYPE html>
<html>
<body>
<p>A typical addition operation adds two numbers and produces a new number.</p>
<script>
var x ;
var y;
var z;
x =100;
y = 200;
z = x+ y;
document.write(" The sum of two numbers is:     " + z);
</script>
</body>
</html>

Try it Yourself »

Question 3

Question:

Write a program to subtract two numbers.


Solution:

<!DOCTYPE html>
<html>
<body>
<p>A typical subtraction operation subtracts two numbers and produces a new number.</p>
<script>
var x ;
var y;
var z;
x=300;
y = 200;
z = x-y;
document.write(" The difference of two numbers is:     " + z);
</script>
</body>
</html>


Try it Yourself »

Question 4

Question:

Write a program to divide two numbers.


Solution:

<!DOCTYPE html>
<html>
<body>
<p>A typical division operation divides two numbers and produces a new number.</p>
<script>
var x ;
var y;
var z;
x=300;
y = 200;
z = x/y;
document.write(" The division of two numbers is:     " + z);
</script>
</body>
</html>




Try it Yourself »

Question 5

Question:

Write a program to multiply two numbers.


Solution:

<!DOCTYPE html>
<html>
<body>
<p> A typical multiplication operation multiplies two numbers and produces a new number.</p>
<script>
var x ;
var y;
var z;
x=300;
y = 200;
z = x* y;
document.write(" The multiplication of two numbers is:     " + z);
</script>
</body>
</html>

Try it Yourself »

Question 6

Question:

Write a program to find the area of a circle.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var r ;
var area;
r=3;
area = 3.14* r* r;
document.write(" The area of the circle is:     " + area +" centimeter square");
</script>
</body>
</html>

Try it Yourself »

Question 7

Question:

Write a program to find the square root of a number.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var x ;
var z;
x=4;
z = Math.sqrt(x);
document.write(" The square root of a number z is: " + z);
</script>
</body>
</html>


Try it Yourself »

Question 8

Question:

Write a program to find the cube root of a number.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var x ;
var z;
x=4;
z = Math.cbrt(x);
document.write(" The cube root of a number z is: " + z);
</script>
</body>
</html>


Try it Yourself »

Question 9

Question:

Write a program to round off a number.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var x ;
var z;
x=4.5;
z = Math.round(x);
document.write(" The round off a number z is: " + z);
</script>
</body>
</html>


Try it Yourself »

Question 10

Question:

Write a program to find the incremented and decremented values of two numbers.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var x ;
var y;
var z;
var p;
var a;
var b;
x=4;
y=6;
z=x+1;
p=x-1;
a = y+1;
b= y-1;
document.write(" The incremented value of  x  is: " + z);
document.write(" The decremented value of  x  is: " + p);
document.write(" The incremented value of  y  is: " + a);
document.write(" The decremented value of  y  is: " + b);
</script>
</body>
</html>


Try it Yourself »

Question 11

Question:

Write a program to find the greatest of two numbers using if – else statement.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var x ;
var y;
x=4;
y=6;
if(x>y){
document.write(" x is greater than y");
} else {
document.write(" y is greater than x");
}
</script>
</body>
</html>

Try it Yourself »

Question 12

Question:

Write a program to print the first ten natural numbers using for loop statement.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var i ;
for (i=1; i<=10; i++)
document.write("" +  i);
</script>
</body>
</html>

Try it Yourself »

Question 13

Question:

Write a program to print the first ten natural numbers using while loop statement.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var i=1 ;
while (i<=10)
document.write("" +  i++);
</script>
</body>
</html>


Try it Yourself »

Question 14

Question:

Write a program to print the first nine natural numbers using do while loop statement.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var i=1 ;
do{
document.write("" +  i++);
} while (i<10)
</script>
</body>
</html>


Try it Yourself »

Question 15

Question:

Write a program to print the average of the first 10 numbers using for loop statement.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var i, avg, sum = 0;
for( i=1; i<=10; i++)
sum = sum + i;
avg = sum/10;
document.write("<br> sum of the first 10 numbers =  </br>" + sum);
document.write("<br> average of the first10 numbers =  </br>" + avg);
</script>
</body>
</html>



Try it Yourself »

Question 16

Question:

Write a program to add two numbers using JavaScript function.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function addition(a, b) {
return a + b;
}
document.write("" + addition(4, 3));
</script>
</body>
</html>



Try it Yourself »

Question 17

Question:

Write a program to display the current date and time.


Solution:

<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html> 

Try it Yourself »

Question 18

Question:

Write a program to find the area of a triangle where lengths of the three of its sides are 5, 6, 7.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var side1 = 5; 
var side2 = 6; 
var side3 = 7; 
var s = (side1 + side2 + side3)/2;
var area =  Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
document.write(area);
</script>
</body>
</html>


Try it Yourself »

Question 19

Question:

Write a program to convert temperature from fahrenheit to celsius.


Solution:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function toCelsius(f) {
  return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius(77);
</script>
</body>
</html>


Try it Yourself »

Question 20

Question:

Write a program to get the website URL (loading page).


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
alert(document.URL);
</script>
</body>
</html>

Try it Yourself »

Question 21

Question:

Write a program to reverse a given string.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function string_reverse(str) 
{
    return str.split("").reverse().join("");
}
document.write(string_reverse("JavaScript"));
</script>
</body>
</html>

Try it Yourself »

Question 22

Question:

Write a program to count the number of vowels in a given string.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function vowel_Count(str)
{ 

  return str.replace(/[^aeiou]/g, "").length; 
}
document.write(vowel_Count("Python")); 
</script>
</body>
</html>


Try it Yourself »

Question 23

Question:

Write a program to find sum of array elements.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
var num = [1,2,3,4]
var sum = 0;
for(var i = 0; i < num.length; i++){
  sum += num[i]
}
document.write(sum);
</script>
</body>
</html>

Try it Yourself »

Question 24

Question:

Write a program to create the dot products of two given 3D vectors.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function dot_product(vector1, vector2) {
  var result = 0;
  for (var i = 0; i < 3; i++) {
    result += vector1[i] * vector2[i];
  }
  return result;
}
document.write(dot_product([1,2,3], [1,2,3]))
</script>
</body>
</html>

Try it Yourself »

Question 25

Question:

Write a program to find the number of even digits in a given integer.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function even_digits(num) {
  var ctr = 0;
  while (num) {
    ctr += num % 2 === 0;
    num = Math.floor(num / 10);
  }
  return ctr;
}
document.write(even_digits(124));
</script>
</body>
</html>

Try it Yourself »

Question 26

Question:

Write a program to change the capitalization of all letters in a given string.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function change_case(txt) {
    var str1 = "";
    for (var i = 0; i < txt.length; i++) {
        if (/[A-Z]/.test(txt[i])) str1 += txt[i].toLowerCase();
        else str1 += txt[i].toUpperCase();
    }
    return str1;
}
document.write(change_case("germany"));
</script>
</body>
</html>

Try it Yourself »

Question 27

Question:

Write a program to remove all characters from a given string that appear more than once.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function remove_duplicate_cchars(str) {
  var arr_char = str.split("");
  var result_arr = [];

  for (var i = 0; i < arr_char.length; i++) {
    if (str.indexOf(arr_char[i]) === str.lastIndexOf(arr_char[i]))
      result_arr.push(arr_char[i]);
    }

  return result_arr.join("");
}
document.write(remove_duplicate_cchars("abcdabc"));
</script>
</body>
</html>



Try it Yourself »

Question 28

Question:

Write a program to sort an array of all prime numbers between 1 and a given integer.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function sort_prime(num) {

  var prime_num1 = [],
      prime_num2 = [];
  for (var i = 0; i <= num; i++) {
    prime_num2.push(true);
  }
  for (var i = 2; i <= num; i++) {
    if (prime_num2[i]) {
      prime_num1.push(i);
      for (var j = 1; i * j <= num; j++) {
        prime_num2[i * j] = false;
      }
    }
  }

  return prime_num1;
}
document.write(sort_prime(11));
</script>
</body>
</html>


Try it Yourself »

Question 29

Question:

Write a program to check whether there is at least one element which occurs in two given sorted arrays of integers.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function check_common_element(arra1, arra2) {
  for (var i = 0; i < arra1.length; i++)
  {
    if (arra2.indexOf(arra1[i]) != -1) 
      return true;
  }
  return false;
}
document.write(check_common_element([1,2,3], [3,4,5]));
</script>
</body>
</html>



Try it Yourself »

Question 30

Question:

Write a program to find the maximum difference between any two adjacent elements of a given array of integers.


Solution:

<!DOCTYPE html>
<html>
<body>
<script>
function max_difference(arr) {
	var max = -1;
    var temp;
	for (var i = 0; i < arr.length - 1; i++)
      {
		temp = Math.abs(arr[i] - arr[i + 1]);
		max = Math.max(max, temp);
	  }
	return max;
}

document.write(max_difference([1, 2, 3, 8, 9]));
</script>
</body>
</html>


Try it Yourself »

javascript-exercises-for-beginners's People

Contributors

manjunath5496 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.