Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
N consecutive odd numbers JavaScript
In this problem statement, our target is to print the n consecutive odd numbers and implement this problem with the help of Javascript functionalities. We can solve this problem using loops and array manipulation in JavaScript.
Logic for the Given Problem
In the given problem statement we have to design a program to generate a given number of consecutive odd numbers starting from 1. It will be done by creating an empty array to store the odd numbers and then using a loop which will run n times to add every odd number to the created array.
The loop will start with the first odd number which is 1 and increment by 2 to get the forthcoming odd number. When the loop finishes, the function will return the array containing the n consecutive odd numbers.
Algorithm
Step 1 ? In this program, we need to generate n consecutive odd numbers so first we need to assign it to a variable called n.
Step 2 ? As we know, the first odd number is always 1 so we will assign it to another variable called begin.
Step 3 ? Now we will create a blank array called oddNums which is used to store the sequence of consecutive odd numbers.
Step 4 ? After defining the above things, we need to loop through the sequence of numbers n times. For every traverse of the loop we will add the current value of begin to the oddNums array. And then increment the value of begin by 2 to get the next odd number in the sequence.
Step 5 ? When the loop gets finished we will return the oddNums array which is now containing the sequence of odd numbers till n.
Method 1: Using For Loop
//function for getting n consecutive odd numbers
function consecutiveOddNumbers(n) {
let begin = 1;
const oddNums = [];
for (let i = 0; i < n; i++) {
oddNums.push(begin);
begin += 2;
}
return oddNums;
}
console.log("First 7 consecutive odd numbers:", consecutiveOddNumbers(7));
console.log("First 5 consecutive odd numbers:", consecutiveOddNumbers(5));
First 7 consecutive odd numbers: [ 1, 3, 5, 7, 9, 11, 13 ] First 5 consecutive odd numbers: [ 1, 3, 5, 7, 9 ]
Method 2: Using While Loop
function consecutiveOddNumbersWhile(n) {
let begin = 1;
const oddNums = [];
let count = 0;
while (count < n) {
oddNums.push(begin);
begin += 2;
count++;
}
return oddNums;
}
console.log("Using while loop:", consecutiveOddNumbersWhile(6));
Using while loop: [ 1, 3, 5, 7, 9, 11 ]
Method 3: Using Array.from()
function consecutiveOddNumbersArray(n) {
return Array.from({length: n}, (_, i) => 2 * i + 1);
}
console.log("Using Array.from():", consecutiveOddNumbersArray(8));
Using Array.from(): [ 1, 3, 5, 7, 9, 11, 13, 15 ]
Comparison
| Method | Readability | Performance | Memory Usage |
|---|---|---|---|
| For Loop | Good | Fast | O(n) |
| While Loop | Good | Fast | O(n) |
| Array.from() | Excellent | Fast | O(n) |
Complexity Analysis
The time complexity for all algorithms is O(n) because we need to generate exactly n odd numbers. The space complexity is also O(n) because we create an array of size n to store the odd numbers.
Conclusion
We explored multiple approaches to generate n consecutive odd numbers in JavaScript. The Array.from() method provides the most concise solution, while traditional loops offer more control and readability. All methods have linear time and space complexity, making them efficient for most use cases.
