AI & Machine Learning

Looping Programs

53 sec read316 views0 listens

The provided JavaScript snippets cover a variety of basic programming tasks and operations. Here's a summary and explanation for each:

1. Prime Numbers

Code:

javascript
1for (let i = 2; i <= 100; i++) { 2 let isPrime = true; 3 for (let j = 2; j < i; j++) { 4 if (i % j === 0) { 5 isPrime = false; 6 break; 7 } 8 } 9 if (isPrime) console.log(i); 10}

Explanation: This code prints all prime numbers between 2 and 100. It checks each number i to see if it's divisible by any number j from 2 up to i-1. If a divisor is found, the number is not prime.

2. Fibonacci Sequence

Code:

javascript
1let n = 10; 2let a = 0, b = 1; 3 4for (let i = 0; i < n; i++) { 5 console.log(a); 6 let temp = a + b; 7 a = b; 8 b = temp; 9}

**Explanation

Read the full article at DEV Community


Want to create content about this topic? Use Nemati AI tools to generate articles, social posts, and more.

Views316
On this page
Ali Nemati
Ali NematiBlogUI.author_articles
BlogUI.view_profile