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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:17+00:00 2026-05-25T11:52:17+00:00

Doing some javascript self-learning and the program itself is very simple. It searches through

  • 0

Doing some javascript self-learning and the program itself is very simple. It searches through the input for keywords and when it finds it, it places the index values into list placeholders. (I’ll develop later code to remove repetitions).

However, a snag. I’m trying to do something very simple. All I’m doing is assigning the variable words5 to keywords[n].length. Now I’ve discovered the keywords[n].length does print me the correct value when nested into document.write(). I’ve also discovered that keywords[n] also prints the correct value when nested into document.write(). BUT if either are used in the code outside of a document.write, it causes the code to break.

Been going at it for hours. And I haven’t been able to find a solution. Do you guys have any clues? The code that I used to test it out is commented out.

<html>
<body>

<script type="text/javascript">


function separate()
{
    contents = document.myForm.event.value;
    placeholders = [32342423, 253234523];
    keywords = [" in ", " at ", " on ", " for "];

    for(n=0;n<=keywords.length;n++)
    {
        for (i=0;i<=contents.length;i++)
        {
            //document.write(i,"+", i+keywords[n].length, keywords[n])
            //document.write(keywords[n].length)
            word5 = (keywords[n].length);
            //document.write(" " + contents.slice(i,i+word5) + " ")
            //if (contents.slice(i,i+4) == " at ")
            //if (contents.slice(i,i+wordlength) == " at ")
            //if (contents.slice(i,i+4) == keywords[n])
            if (contents.slice(i,i+word5) == keywords[n])
            {
                placeholders.push(i);
            }
        }
    }
    document.getElementById("sliced").innerHTML = placeholders;
    printer();
}


function printer()
{
    contents = document.myForm.event.value;
    document.getElementById("MSG").innerHTML = contents;
}
</script>



<form name="myForm" method="post">
Add Event: <input type="text" name="event" value="Whatever at hello at crazy" /><br />
<input type=button value="Submit" onClick="separate()" />
</form>

<SPAN ID="sliced">&nbsp;</SPAN>
<p></p>
<SPAN ID="MSG">&nbsp;</SPAN>



</body>
</html>

Any help is greatly appreciated. Saviours really =)

  • 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-05-25T11:52:18+00:00Added an answer on May 25, 2026 at 11:52 am

    that’s not what’s breaking your code, it seems like you are not defining the variable “wordlength” anywhere on your code, this is what’s breaking your code.

    word5 = (keywords[n].length) actually works.

    Also I would recommend getting “firebug” addon for firefox, it’s the best way to debug any javascipt or html code, use the console to output your debug statements using “console.log(myvar)” instead of document.write which is a pretty old fashioned way of doing things.

    Let me know if this solution doesn’t work for you 🙂

    EDIT:
    I did another set of tests and found out what the reason was, your first loop is running 1 more than what your current number of arrays are, so instead of making it “less than or equal too” it should just be less than:

    for(n=0;n<keywords.length;n++)

    Hence why you’re getting a keywords[n] is undefined, as there is no keywords[4] in the array

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

Sidebar

Related Questions

I am doing some self learning about Patern Matching in Javascript. I got a
I am doing some self-learning about JQuery. Here is the code for my very
While doing some JavaScript performance tests I came up with the following piece of
I am working with ASP.NET doing some client side javascript. I have the following
I'm doing some straight up asynchronous calls from javascript using the XMLHTTPRequest object. On
I'm doing some experimenting with this malicious JavaScript line: var undefined = true; Every
I'm doing some maintenance coding on a webapp and I am getting a javascript
I came across a strange behaviour when doing some regular expressions in JavaScript today
I would like to compare two dates in javascript. I have been doing some
While doing some random experimentation with a factorial program in C, Python and Scheme.

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.