Variable Arguments in JavaScript

Free Web development courses with real-time projects Start Now!!

Program 1

// Variables arguments in funcion 

function displaysort(...args)
{
        let i,j,temp
        for(i=0;i<args.length;i++)
        {
            for(j=i+1;j<args.length;j++)
            {
                 if(args[j]<args[i])
                 {
                        temp=args[i]
                        args[i]=args[j]
                        args[j]=temp
                 }
            }
        }
      console.log("Sorted elements : ")
      args.forEach(element => 
        {
           console.log(element)
      });   
}

displaysort(4,2,1,5,3)







// "use strict"
// const ps=require("prompt-sync")
// const prompt=ps({sigint:true})

//  let m=parseInt(prompt("Enter a number for search: "))
//  let x;
//  x=searchData(m,10,20,30,40,50,60,70,80)

//  if(x)
//   console.log("searching success")
// else
// console.log("searching not success")


// function searchData(s,...args)  
//  {
//        for(let i=0;i<args.length;i++)
//        {
//           if(parseInt(args[i])==parseInt(s))
//                      return true
//        }
//        return false


//  }


// function sum(...args)
// {
//        let sum=0;
//         args.forEach(n => 
//         {
//             console.log(n)
//             sum=sum+n      
//         });
//         return sum;
// }

// s=sum(10,20,30,40,50)
// console.log("sum is: " ,s)

// function display(x,y,...args)  
// {
//         args.forEach(n=> {
//               console.log(n)
//         });
// }

//   console.log(display.length);

//display(5,7,10,20,30,40)

// display(10,30)  // two 
//  console.log("-----------------------")
// display(10,20,30); //three
//  console.log("-----------------------")
//  display(10,20,30,40); //four
//  console.log("-----------------------")

// for(i=0;i<args.length;i++)           
        //     console.log(args[i])

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *