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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:46:34+00:00 2026-06-19T02:46:34+00:00

function Todo(id, task, who, dueDate) { this.id = id; this.task = task; this.who =

  • 0
function Todo(id, task, who, dueDate) {
    this.id = id;
    this.task = task;
    this.who = who;
    this.dueDate = dueDate;
    this.done = false;
}    

// more code that adds the todo objects to the page and to the array todos

function search() {
    for (var i = 0; i < todos.length; i++) {
        var todoObj = todos[i];
        console.log(todoObj.who); //shows both jane and scott
        console.log(todoObj.task); // shows both do something and get milk
    }
    var searchTerm = document.getElementById("search").value;
    searchTerm = searchTerm.trim();
    var re = new RegExp(searchTerm, "ig");
    var results = todoObj.who.match(re);
    if (searchTerm == null || searchTerm == "") {
        alert("Please enter a string to search for");
        return;
    } else {
        alert(results);
    }
} 

This is a search function where I am trying to match what the user types into the search bar with objects that I have created earlier in the code. They must match the “who” and “task” parameters that I have given to the objects. So one object is who: jane task: do something and the other is who: scott task: get milk. The problem is, in my last alert I can only match scott and not jane. Scott is the last one I added. Is there some way I need to modify my loop or change my search criteria?

  • 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-19T02:46:35+00:00Added an answer on June 19, 2026 at 2:46 am

    Your problem is that you are looping through the items, but then using todoObj after that loop. So todoObj will just hold the last item in the array. You need to reorganize a little…try something like this:

    function search() {
        var searchTerm = document.getElementById("search").value;
        searchTerm = searchTerm.trim();
    
        if (searchTerm == null || searchTerm == "") {
            alert("Please enter a string to search for");
            return;
        } else {
            var todoObj = undefined,
                results = undefined,
                re = new RegExp(searchTerm, "ig");
    
            for (var i = 0; i < todos.length; i++) {
                todoObj = todos[i];
                results = todoObj.who.match(re);
                if (results) {
                    alert("You found " + todoObj.who + ", who needs to " + todoObj.task + " by " + todoObj.dueDate);
                    return;
                }
                console.log(re.lastIndex);
            }
    
            alert("You didn't match anyone");
        }
    }
    

    Here’s an example of it working as I think you want it to: http://jsfiddle.net/sHSdK/2/

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

Sidebar

Related Questions

function Todo(id, task, who, dueDate) { this.id = id; this.task = task; this.who =
$(.eventName).click( function () { var eventId = $(this).attr('id'); var role = $(this).attr('dir'); var todo
I'm trying to add controller to my Todo list app. Here's the code. $(function(){
I've got a function to do ANOVA for a specific column (this code is
There must be an easier way or function to do this code here: #!/usr/bin/env
Programming language: Java Task: designing a hash function that maps Chinese Strings to numbers
Why does this code hide the div that's used as the jQuery selector on
I'm trying to code a todo list form. this form can contain unlimited rows.
I need this function to work after the page is loaded. The error is
part of my code in listForm.class.php: public function configure() { $list_id = $this->getOption('list_id'); $endkunde_id

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.