Thursday 3 August 2023

Javascript: How to print numbers from 1 to 10 using javascript setInterval?

 Javascript: How to print numbers from 1 to 10 using javascript setInterval?


Solution: This is basic interview questions.  

setInterval() method is used to call a function or evaluate an expression at specified intervals (in milliseconds). For example, let's log a message after 1 seconds using setInterval method,

setInterval(function () {
  console.log("Hello Javascript");
}, 1000);
Solution for above Question is:
let i =1;
let basicint = setInterval(function () {
  console.log("My count is:",i);
    i++;
    if(i>10){
        clearInterval(basicint); // Here clearing the interval after 10 iterations
    }
}, 1000);


Finally the out is:


 Javascript: How to print numbers from 1 to 10 using javascript setInterval?


Solution: This is basic interview questions.  

setInterval() method is used to call a function or evaluate an expression at specified intervals (in milliseconds). For example, let's log a message after 1 seconds using setInterval method,

setInterval(function () {
  console.log("Hello Javascript");
}, 1000);
Solution for above Question is:
let i =1;
let basicint = setInterval(function () {
  console.log("My count is:",i);
    i++;
    if(i>10){
        clearInterval(basicint); // Here clearing the interval after 10 iterations
    }
}, 1000);


Finally the out is:


Javascript: Interview Question: How to filter even numbers from Array without using predefined filter in Javascript?

 Javascript: Interview Question: How to filter even numbers from Array without using predefined filter in Javascript?



Solution: By using for loop .. we can achieve this easily

let arrNums = [46,10,21, 56, 89, 67, 33, 87, 34, 98, 76, 88, 65, 12, 19, 10, 15, 17, 18,22];

let evenNums = [];

for(let i=0;i<=arrNums.length;i++){

    if (arrNums[i] % 2 === 0) {

        evenNums.push(arrNums[i]);

   }

}

console.log(evenNums)


Out Put : 

[46, 10, 56, 34, 98, 76, 88, 12, 10, 18, 22]

 Javascript: Interview Question: How to filter even numbers from Array without using predefined filter in Javascript?



Solution: By using for loop .. we can achieve this easily

let arrNums = [46,10,21, 56, 89, 67, 33, 87, 34, 98, 76, 88, 65, 12, 19, 10, 15, 17, 18,22];

let evenNums = [];

for(let i=0;i<=arrNums.length;i++){

    if (arrNums[i] % 2 === 0) {

        evenNums.push(arrNums[i]);

   }

}

console.log(evenNums)


Out Put : 

[46, 10, 56, 34, 98, 76, 88, 12, 10, 18, 22]

Wednesday 2 August 2023

Javascript: How to Print Tables from 1 to 20 using JavaScript?

Javascript: How to Print Tables from 1 to 20 using JavaScript?

The simple way to print the Math tables from 1 to 20 using javascript. This is basic logic of loops in javascript. 

Sample:

let tableNo = 2;
console.log("Multiplication Table of", tableNo, "is:");
for(let i=1; i<=10; i++)
{
    console.log(tableNo +"*"+ i +"="+ tableNo*i);
    if(i==10 && tableNo<20)
    {
        tableNo = tableNo+1;
        i=0;
        console.log("=================================");
        console.log("Multiplication Table of", tableNo, "is:");
    }
}




 OutPut : The out put like this
Javascript: How to Print Tables from 1 to 20 using JavaScript?

The simple way to print the Math tables from 1 to 20 using javascript. This is basic logic of loops in javascript. 

Sample:

let tableNo = 2;
console.log("Multiplication Table of", tableNo, "is:");
for(let i=1; i<=10; i++)
{
    console.log(tableNo +"*"+ i +"="+ tableNo*i);
    if(i==10 && tableNo<20)
    {
        tableNo = tableNo+1;
        i=0;
        console.log("=================================");
        console.log("Multiplication Table of", tableNo, "is:");
    }
}




 OutPut : The out put like this

Quick Solutions

More

About Us

Hello!
I`m Sharath.
I'll design & develop dynamic websites,web applications, and anything related to web. I'll develop projects in JAVA,.Net, PHP and Android Apps .

Contact Us

Sharath Babu
Company : SLS Tech Services.
URL : SLS Tech Services
Top