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 8970505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:44:09+00:00 2026-06-15T17:44:09+00:00

I have this text ( ) ( ( ) ( ( ) ) (

  • 0

I have this text

( ) ( ( ) ( ( ) ) ( ( ) ) )

Pratically each bracket is a span and i have writed this code to organize the nested structure:

myobj.each(function(i) { 

    var isOpen = $( this ).text( ) == '(' ? true: false;

    if( isOpen ) {
        console.log( ' open at: ' + index  )
        index++;
        closer = index;
    } else {
       closer--;
       console.log( 'close at: ' + ( closer ) );
    }

});

Assuming that the expeted output would be:

0 0 1 2 2 3 4 4 3 5 6 6 5 1
( ) ( ( ) ( ( ) ) ( ( ) ) )

it give me:

0 0 1 2 2 3 4 4 3 5 6 6 5 4
( ) ( ( ) ( ( ) ) ( ( ) ) )

note the last bracket.. Why?

  • 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-15T17:44:10+00:00Added an answer on June 15, 2026 at 5:44 pm

    Store the unclosed (just opened) bracket’s index in an array, then use pop to get the last unclosed one when closing.

    var index = -1, unclosed = [], log = [];
    myobj.each(function() {
        if ($(this).text() === '(') {
            index++;
            console.log('Open at: ' + index);
            unclosed.push(index);
            log.push(index);
        } else {
            var closer = unclosed.pop();
            console.log('Closed at: ' + closer);
            log.push(closer);
        }
    });
    console.log(log); //[0, 0, 1, 2, 2, 3, 4, 4, 3, 5, 6, 6, 5, 1]
    

    Fiddle

    The log above is just for showing the final result, those 3 lines + declaration can later be removed.

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

Sidebar

Related Questions

I have this piece of code written in jQuery and PHP: <script type=text/javascript> $(document).ready(function(){
I have this following jquery text fly-in animation.Here is my code before I explain
I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I
I have a user control with this code behind: /// <summary> /// The text
I have this text read into an NSString *responce which I am trying to
I have this input text and an input button...I am looking to have them
I have this input text and input button, I am trying to get them
I have this RTF text: {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}} {\colortbl ;\red0\green0\blue0;\red255\green0\blue0;} \viewkind4\uc1\pard\qc\cf1\fs16 test \b bold \cf2\b0\i
I have this little text <div> Hello world<br> this is a good<br> text! <br><br>
I have got this text file with latitude and longitude values of different points

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.