How to use JavaScript Spread (…)Operator

Program to use JavaScript Spread (…)Operator


let a = [1,2,3,4,5];
let b = [6,7,8];
let c = [...a,...b]
console.log(c);

Output


[
    1, 2, 3, 4,
    5, 6, 7, 8
]

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables with array value respectively a = [1,2,3,4,5], b = [6,7,8] and c = [...a,...b]
  3. The spread(...) operator converts multiple arrays into a single array.
  4. The value of c variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Spread (...)Operator


let d = [1,2,3,4,5];
let e = [6,7,8];
let f = [9,10];
let g = [...d,...e,...f];
console.log(g);

Output


[
    1, 2, 3, 4,  5,
    6, 7, 8, 9, 10
]

Steps Description

  1. Declare Four variables d, e, f and g
  2. Initialize Four variables with array value respectively d = [1,2,3,4,5], e = [6,7,8], f = [9,10] and g = [...d,...e,...f]
  3. The spread(...) operator converts multiple arrays into a single array.
  4. The value of g variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Spread (...)Operator


let h = ['Apple', 'Mango', 'Banana'];
let i = ['Orange', 'Pomegranate'];
let j = [...h,...i];

console.log(j);

Output


[ 'Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate' ]

Steps Description

  1. Declare three variables h, i and j
  2. Initialize three variables with array value respectively h = ['Apple', 'Mango', 'Banana'], i = ['Orange', 'Pomegranate'] and j = [...h,...i]
  3. The spread(...) operator converts multiple arrays into a single array.
  4. The value of j variable is displayed with the help of console.log inbuilt function

How to use Armstrong number in C Language

Program to use Armstrong number in C Language


#include <stdio.h>
int main(void)
{
    int num, n, cube, d, sum;
    printf("armstorng numbers are :\n");
    for (num = 100; num <= 999; num++)
    {
        n = num;
        sum = 0;
        while (n > 0)
        {
            d = n % 10;
            n /= 10;
            cube = d * d * d;
            sum = sum + cube;
        }
        if (num == sum)
            printf("%d\n", num);
    }
    return 0;
}

output


armstrong number are :
153
370

How to use JavaScript Array filter()

Program to use JavaScript Array filter() method


let a = ['Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate'];

let b = a.filter(a => a.length > 5);

console.log(b);

Output


[ 'Banana', 'Orange', 'Pomegranate' ]

Program to use JavaScript Array filter() method


let c = ['Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate'];

let d = ['Orange', 'Apple', 'Banana']

let e = (c, d) => {
    let value = [...c, ...d];
    return value.filter((el) => {
        return !(c.includes(el) && d.includes(el));
    });
};

console.log(e(c, d));

Output


[ 'Mango', 'Pomegranate' ]

Program to use JavaScript Array filter() method


let f = [1, 2, 3, 4];
let g = [2, 1, 8, 3];
let h = (f, g) => {
  let value = [...f, ...g];
  return value.filter((el) => {
    return !(f.includes(el) && g.includes(el));
  });
};
console.log(h(f, g));

Output


[ 4, 8 ]

How to use JavaScript Array slice()

Program to use JavaScript Array slice() method


let a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];

let b = a.slice(1,4);

console.log('String slice =',b);

Output


String slice = [ 'Dog', 'Tiger', 'Lion' ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of array let a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ] and b
  3. slice() method is used to slice the array by giving index number
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array slice() method with multiplication table


let c = [ 3,6,9,12,15,18,21,24,27,30 ];

let d = c.slice(5);

console.log('String slice =',d);

Output


String slice = [ 18, 21, 24, 27, 30 ]

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of array let c = [ 3,6,9,12,15,18,21,24,27,30 ] and d
  3. slice() method is used to slice the array by giving index number
  4. The value of d variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array slice() method with count


let e = [ 1,2,3,4,5,6,7,8,9,10 ];

let f = e.slice(3,6);

console.log('String slice =',f);

Output


String slice = [ 4, 5, 6 ]

Steps Description

  1. Declare two variable e and f
  2. Initialize two variable e and f with value of array let e = [ 1,2,3,4,5,6,7,8,9,10 ] and f
  3. slice() method is used to slice the array by giving index number
  4. The value of f variable is displayed with the help of console.log inbuilt function

How to use JavaScript String split()

Program to use JavaScript String split() method


let a = "Hello World JavaScript";

let b = a.split("");

console.log(b);

Output


[
    'w', 'a', 'n', 't', ' ', 't',
    'o', ' ', 'c', 'o', 'd', 'e',
    ' ', 'j', 'a', 'v', 'a', 's',
    'c', 'r', 'i', 'p', 't', ' ',
    'w', 'i', 't', 'h', ' ', 'm',
    'e'
  ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of string a = "Hello World JavaScript"; and b
  3. The split() method converts a string into an array.
  4. If we split a string without space with the help of split method, it will turn into a string array
  5. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String split() method


let c = "Hello World JavaScript";

let d = c.split(" ");

console.log(d);

Output


[ 'Hello', 'World', 'JavaScript' ]

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of string c = "Hello World JavaScript"; and d
  3. The split() method converts a string into an array.
  4. The value of d variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String split() method


let e = "Hello. World. JavaScript";

let f = e.split(".");

console.log(f);

Output


[ 'Hello ', ' World ', ' JavaScript' ]

Steps Description

  1. Declare two variable e and f
  2. Initialize two variable e and f with value of string e = "Hello. World. JavaScript" and f
  3. The split() method converts a string into an array.
  4. The value of f variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String split() method


let g = "Hello World JavaScript";

let h = g.split(" ");

console.log(h[2]);

Output


javascript

Steps Description

  1. Declare two variable g and h
  2. Initialize two variable g and h with value of string g = "Hello World JavaScript" and h
  3. The split() method converts a string into an array.
  4. The value of h variable is displayed with the help of console.log inbuilt function

How to use sum of digits of any number for loop in C Language

Program to use sum of digits of any number for loop in C Language


#include <stdio.h>
int main()
{
    int n, sum = 0, rem;
    printf("enter a number : ");
    scanf("%d", &n);
    for (; n > 0; n /= 10)
    {
        rem = n % 10;
        sum += rem;
    }
    printf("sum of digits=%d\n", sum);
    return 0;
} 

output


enter a number : 433
sum of digits=10