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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:24:13+00:00 2026-05-30T16:24:13+00:00

I am new to programming and javascript and i have a question ive been

  • 0

I am new to programming and javascript and i have a question ive been struggling with for a bit now. Im trying to build a card game and have a card object which returns a card representation of the format (card #,suit eg. 5,Hearts). I deal a card at the start of the program and validate that the card dealt is unique (ie not been used already). I have the following structure:

var usedCards= [];

function dealCards() {
  for (i = 0; i < 3; i++)
  { 
    var card = createUniqueCard();
    usedCards.push(card.cardRepresentation);
  }
}

function createUniqueCard() {
  do {
    var newCard = new Card();
  }
  while (usedCards.indexOf(newCard.cardRepresentation) != -1);

  return newCard;
}

This still returns me duplicate cards in my usedCards array. Can anyone point out my logic error?

Thanks

  • 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-30T16:24:14+00:00Added an answer on May 30, 2026 at 4:24 pm

    If newCard.cardRepresentation is an object then .indexOf() will never find a match because two object references are considered equal only if they refer to the same instance – you keep creating new instances with new Card().

    If you can have newCard.cardRepresentation as a string it should work. Or if you write your own function to replace .indexOf(), where your function knows how to compare two .cardRepresentation objects…

    I think a better solution is to begin by generating all possible cards, putting them in an array, and then your deal function could randomly select from that array.

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

Sidebar

Related Questions

I'm somewhat new to object oriented programming in Javascript and I'm trying to build
I am completely new to javascript programming and I have a question that I
i been thinking of a new programming language. Before trying to implement it i
I'm relatively new to web programming. New to Javascript, PHP, Ajax, etc. I have
I am a bit new to programming in jQuery/Javascript and am having some issues
I'm quite new to javascript programming and got stuck in this problem: I have
I'm trying to learn to apply object-oriented principles to my Javascript programming, however I'm
I'm pretty new to javascript and programming and have run into a brick wall
I'm new to programming but have managed to build a rather complex dynamic site
I am new to javascript programming. I have a page on which if Back

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.