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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:46:28+00:00 2026-06-16T15:46:28+00:00

I am trying out javascript for (more-or-less) the first time and find myself completely

  • 0

I am trying out javascript for (more-or-less) the first time and find myself completely baffled by the following .js script.

var pair = newArray();
var id = newArray();
var pairs = 2;

function newGame(){

    var randomid = 0;

    alert("newGame() called!");

    // Sets a specific part of the image sprite to each pair[].
    for (var i=0 ;i < pairs; i++){
        alert("For loop started!");
        pair[i] = "url(Cardfront.jpg) -"+100 * Math.floor((Math.random()*13)+0)+"px -"+200 * Math.floor((Math.random()*4)+0)+"px";
        // For every pair, assigns a part of the image sprite to two id[]-s.
        alert("Pair " + i + "is " + pair[i]);
        for(var j=0; j < 2; j++) {
            //the range of possible id-s the total number of cards - double the amount of pairs.
            randomid = Math.floor((Math.random()*pairs*2)+0);
            if (id[randomid] === null){
                id[randomid] = pair[i];
                alert("ID " + randomid + "is " + id[randomid]);
            }
            else j--;
        }
    }

    alert("This is called after the for loop!");
}

When I call newGame() through a button, I receive the “newGame() called!” and then “This is called after the for loop!” alerts, then nothing.

I’ve spent a while googling and poking around trying to figure this out, but I’m at the end of my wits, it seems.

  • 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-16T15:46:29+00:00Added an answer on June 16, 2026 at 3:46 pm

    Change newArray() to new Array() I believe that is what is causing your error sir.

    Good luck!

    EDIT: To fix the other error I found you have the following:

    if (id[randomid] === null) {
    

    with 3 = sign. Change it to just two:

    if (id[randomid] == null) {
    

    and it should work the way you expect it to.
    Unless you are really trying to use the strict comparisson operator, then there is something else bugging your code.

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

Sidebar

Related Questions

I'm trying to find out more about closures in Javascript and was going through
I am trying to figure out some way to get actual time via JavaScript,
I am trying to figure out exactly why this script wont work. I first
I'm working with Javascript/jQuery and I'm trying to figure out what is more advantageous.
I am trying out ajax file upload in django .I coded the javascript ,django
Very confused here, trying out the yuicompressor on a simple javascript file. My js
In trying to keep with unobtrusive JavaScript guidelines I moved all the JavaScript out
I am trying to swap out a hover image with javascript but it doesn't
i am trying to figure out how to create a javascript alert box asking
I'm trying to figure out if there is a way (using JavaScript, or jQuery

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.