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

  • Home
  • SEARCH
  • 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 1838626
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:30:14+00:00 2026-05-17T06:30:14+00:00

Can I do the following? function contains(element) { // if the element is a

  • 0

Can I do the following?

function contains(element) {

 // if the element is a Vertex object, do this
 if (element instanceof Vertex) {

  var vertex = element;
  for ( var index in self.verticies) {
   if (self.verticies[index].id == vertex.id) {
    return true;
   }
  }
  return false;
 }
 // else if the element is an Edge object, do this 
 else if (element instanceof Edge) {

  var edge = element;
  for ( var index in self.verticies) {
   if (self.verticies[index].id == edge.id) {
    return true;
   }
  }
  return false;
 } else {
  // shouldn't come here
  return false;
 }
};

Basically… I want to be able to call contains() and pass it either a Vertex object or an Edge object but I don’t want to have duplicate code. Is this the right way to do it? Furthermore, am I handling the assignment var vertex = element / var edge = element correctly? I want to assign element to another Vertex/Edge object and use that for my look up.

Let me know if I need to clarify.

Thanks,
Hristo

  • 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-17T06:30:15+00:00Added an answer on May 17, 2026 at 6:30 am

    Your code should work fine.

    Note, however, that there is no point (other than clarity, which is a good thing) in writing var edge = element.
    Javascript variables are untyped; there is no difference between edge and element.

    Also, you should probably throw an exception instead of

    // shouldn't come here
    return false;
    

    Finally, why are you searching self.verticies for an Edge?

    Note, by the way, that you still have duplicate code.
    You can rewrite your function like this:

    function contains(element) {
        var searchSet;
    
        // if the element is a Vertex object, do this
        if (element instanceof Vertex) 
            searchSet = self.verticies;
        else if (element instanceof Edge)
            searchSet = self.edges;
        else
            throw Error("Unexpected argument");
    
        for (var i = 0; i < searchSet.length; i++) {
            if (searchSet[i].id == element.id) 
                return true;
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone help with this? I need the following function to do this... $x
http://www.cplusplus.com/reference/algorithm/for_each/ Unary function taking an element in the range as argument. This can either
One of my backbone view's initialize function contains the following code: $.when(this.broadcasters.fetch(), this.model.fetch(), this.call_types.fetch()).done(
How can the following function be implemented in various languages? Calculate the (x,y) point
I know the following function can be used to do some pre-save processing. But,
I have the following function - it works fine in Firefox: How I can
I would like to know if I can use the following JQuery function on
Can anyone describe the following php function: function get_setting_value($settings_array, $setting_name, $default_value = ) {
In the following jquery function, can someone explain to me why second is being
Let's consider the following scenario: a function which can generate code colors from white

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.