Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:48:09+00:00 2026-06-04T13:48:09+00:00

Given an array of unsorted positive ints, write a function that finds runs of

  • 0

Given an array of unsorted positive ints, write a function that finds runs of 3 consecutive numbers (ascending or descending) and returns the indices where such runs begin. If no such runs are found, return null.

function findConsecutiveRuns(input:Array):Array

Example: [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7] would return [0, 4, 6, 7]

My JS skills are a bit rusty, here is my attempt at this…

var numArray = [1, 2, 3, 5, 10, 9, 8, 9, 10, 11, 7];
var newNumArray = []; 

for(var i = 1; i < numArray.length; i++) {
    if ((numArray[i] - numArray[i-1] != 1) || (numArray[i] + numArray[i+1] !=1)  {
        return 0;
    }
    else {
    newNumArray.push(numArray[i]);
    }
}
alert(newNumArray);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:48:11+00:00Added an answer on June 4, 2026 at 1:48 pm

    Here:

    function f ( arr ) {
        var diff1, diff2, result = [];
    
        for ( var i = 0, len = arr.length; i < len - 2; i += 1 ) {
            diff1 = arr[i] - arr[i+1];
            diff2 = arr[i+1] - arr[i+2];
            if ( Math.abs( diff1 ) === 1 && diff1 === diff2 ) {
                result.push( i );
            }        
        }
    
        return result.length > 0 ? result : null;
    }
    

    Live demo: http://jsfiddle.net/Cc4DT/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an unsorted array of numbers, write a function that returns true if array
You are given as input an unsorted array of n distinct numbers, where n
Given an unsorted integer array, and without making any assumptions on the numbers in
I had this question: Given an unsorted array of positive integers and an integer
I have the following script that dumps a given array's contents function dump(obj) {
Question: Given an unsorted array of positive integers, is it possible to find a
You are given an Array of numbers and they are unsorted/random order. You are
Is there a function out there to make sure that any given array conforms
Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
How to do that without having to scroll the entire given array with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.