Javascript practice exercises with solutions pdf

1. Write a JS code to print numbers from 1 to 10

Function `printNumbers()` prints numbers from 1 to 10 using for loop.

function printNumbers() {
  for(var i=1;i<=10;i++){
    console.log(i);
  }
}

printNumbers() //1 2 3 4 5 6 7 8 9 10

2. Write a JS code to print a 2D array

Function `printArray()` prints all the elements of a 2D array using nested for loops.

function printArray(arr) {
  for (var i=0;i

3. Write a JS code to print Even numbers in given array

Function `printEven()` prints all the even numbers of a 2D array using for loops and ‘%’ operator.

function printEven(arr) {
  for (var i=0;i

4. Write a JS code to delete all occurrence of element in given array

Function `deleteElement()` deletes all the occurrence of element from the given array.

function deleteElement(arr, ele) {
  for (var i=0;i

5. Write a JS code to demonstrate Async loop

For loop consisting of setTimeout() function to print loop variable 5 times in asynchronous way.

for(var i=0;i<5;i++){
  setTimeout(()=>console.log(i), 5000);// 5 5 5 5 5
}

6. Write a JS code to find the power of a number using for loop

Function numPower() to returns power of number for provided exponential value using for loop.

function numPower(num,pow) {
  var res=1; //return 1 for pow=0
  for(var i=0;i

7. Write a JS code to print a pattern using for loop

Function printPattern() is used to print a pattern for a given range using nested for loop.

function printPattern(range) {
  for(var i=1;i<=range;i++){
    var str="";
    for(var j=1;j<=i;j++){
      str += j+" ";
    }
    console.log(str);
  }
}
printPatter(8);

/* 1 
   1 2 
   1 2 3 
   1 2 3 4 
   1 2 3 4 5 
   1 2 3 4 5 6 
   1 2 3 4 5 6 7 
   1 2 3 4 5 6 7 8 */

8. Write a JS code to find the no of digits in a number

Function digitCount() to returns the number of digits in a given number using while loop.

function digitCount(num) {
  var count=0; //return 1 for pow=0
  while(num!=0){
    num=Math.floor(num/10);
    count++;
  }
  return count;
}
console.log(digitCount(8367)); //4
console.log(digitCount(563349)); //6

9. Write a JS code to calculate the sum of digits in a number

Function digitSum() to returns sum of all digits in a given number using while loop. Learn more about JavaScript built-in Math methods from developer.mozilla.org/Global_Objects/Math

function digitSum(num) {
  var sum=0;
  while(num!=0){
   sum += num % 10;
   num = Math.floor(num/10);
  }
  return sum;
}
console.log(digitSum(4367)); //20
console.log(digitSum(56349)); //27

10. Write a JS code to find the largest number in an array

Program to find the largest number in the given 1D array. 

var arr = [2, 45, 3, 67, 34, 567, 34, 345, 123];  
var largest = arr[0];
for(var i=0;ilargest ? arr[i]:largest; //Check largest number
}
console.log(largest) //567

11. Write a JS code to find the number of zeros in 2D Matrix

Program to find count number for zeros in 2d matrix using nested for loops and increment operation.

var arr = [[0,1,1],[0,1,0],[1,0,0]];
var zeroCount = 0;
for(var i=0;i

12. Write a JS code to find product of two arrays

Function findProd() to generate a new array which is the product of two arrays of the same size using for loop.

function findProd(arr1, arr2) {
  var arrProd = arr1.length>arr2.length ? arr1 : arr2;
  for(var i=0; i < Math.min(arr1.length, arr2.length); i++){
     arrProd[i] = arr1[i] * arr2[i]; //Product of 2 array elements
  }
  return arrProd;
}
var arr1 = [3,45,23,78,34];
var arr2 = [4,2,34,4,12,1];

console.log(findProd(arr1,arr2)); //[12, 90, 782, 312, 408, 1]

13. Write a JS code to print the Fibonacci series for a given value of N

The function fibonacci() prints the Fibonacci series for the given range N using While loop.

function fibonacci(n) {
  var n1=0;
  var n2=1;
  var count=2;    
  var n3;
  console.log(n1,n2);
  while(count

14. Write a JS code to find N value in the Fibonacci series for a given number

The function Findfibonacci() prints the index of number in the Fibonacci series if present or “element not present” if the number is not part of the Fibonacci series.

function findFibonacci(num) {
  var n1=0;
  var n2=1;
  var count=2;
  var n3;
  while(true){
    n3=n1+n2;
    if(n3==num){
       console.log(`Element ${num} is present in index ${count}`)
       break;
     }     
     else if(n3>num){
       console.log("Element not present");
       break;
     }
     n1=n2;
     n2=n3;
     count++;
  }
}

findFibonacci(13) //"Element 13 is present in index 7"
findFibonacci(144) //"Element 144 is present in index 12"

15. Write a JS code to count all letters in a word

Program to find the count of all letters in a word using double for loops.

var str="Suggesting";
var count=new Array(26);
str=str.toLowerCase();
for(var i=0; i

16. Write a JS code to find duplicate values in a given array

Function findDup() to returns all the elements that are repeated more than once in a given array.

function findDup(arr) {
  var arrDup=[]
  for(var i=0; i

17. Write a JS code for binary search algorithm

Program to find the index of a search element in an array using the binary search algorithm.

var arr = [13,45,34,2,56,3,57,34,88,55];
var key=57;
var low=0;
var high=arr.length-1;
var mid=0;
var flag=0;
arr.sort()

console.log("The sorted array is: "+arr); //13,2,3,34,34,45,55,56,57,88

while(low<=high)
{
  mid=Math.floor((low+high)/2);
  if(key < arr[mid])
  {
    high=mid-1;
  }
  else if(key > arr[mid])
  {
    low=mid+1;
  }
  else if(key == arr[mid])
  {
    flag++;
    console.log("found at index:"+mid); //print the position
    break;
  }
}

if(flag==0)
{
  console.log("Not found"); // Element not present in array
}

JavaScript Loop tutorials

How can I Practise JavaScript?

More videos on YouTube.
Build Javascript Projects. ... .
Improve Existing Projects. ... .
Complete Code Challenges. ... .
Join an Open Source Project. ... .
Join the Coding Community. ... .
Share your Javascript Learning Journey with Others. ... .
Write Coding Articles and Share Knowledge..

Where can I practice JavaScript problems?

Codedamn is an interactive programming platform; you will learn and build projects side by side. It offers two courses to practice and master JavaScript.

How can I improve JavaScript?

Let's get started:.
Keep Your Basics Clear. ... .
Practice As Much As You Can. ... .
Stay Up to Date. ... .
Remember the Rules of Programming. ... .
Apply Object-Oriented Approach. ... .
Write, Test, and Get Perfect. ... .
Handling Error is Important. ... .
Create Different Projects..

Can I practice JavaScript on my phone?

The answer is obviously yes. To write a script in native javascript, you don't any special IDE exclusively developed for writing javascript. Any of your favorite text editor will be good for writing JavaScript.