
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
N consecutive odd numbers JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument, and a number, say n, as the second argument.
The function should return true if there exist n consecutive odd numbers in the array, false otherwise.
For example −
If the input array and number are −
const arr = [3, 5, 3, 5, 4, 3]; const n = 4;
Then the output should be true because first four numbers are all odd.
Example
const arr = [3, 5, 3, 5, 4, 3]; const n = 4; const allOdd = (arr = [], n = 0) => { if(!arr.length){ return; }; let streak = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el % 2 === 0){ streak = 0; } else{ streak++; }; if(streak === n){ return true; } }; return false; }; console.log(allOdd(arr, n));
Output
This will produce the following output −
true
- Related Articles
- Squared sum of n odd numbers - JavaScript
- Express the following numbers as the sum of consecutive odd numbers: $36$.
- Check three consecutive numbers - JavaScript
- What is the HCF of two consecutive(a) numbers? (b) even numbers? (c) odd numbers?
- Sum of consecutive numbers in JavaScript
- Average of first n odd naturals numbers?
- The sum of the squares of two consecutive odd numbers is 394. Find the numbers.
- The sum of three consecutive odd numbers is 39. Find the number
- JavaScript to check consecutive numbers in array?
- Finding three desired consecutive numbers in JavaScript
- Sum of square of first n odd numbers
- Adding only odd or even numbers JavaScript
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Largest Even and Odd N-digit numbers in C++
- Find the sum of first $n$ odd natural numbers.

Advertisements