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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:46:58+00:00 2026-06-13T11:46:58+00:00

I am currently adding extra sounds to my game. The problem is I have

  • 0

I am currently adding extra sounds to my game. The problem is I have a shorthand if statement like “?…:…” and don’t know how I can add audio to it. If it is not possible how would I make it a normal if statement without making it crash.

    b.clone().addClass(
    b.data("letter") == target.data("letter") ? "wordglow3" : "wordglow").appendTo("table").css({
        background: "transparent",
        position: "absolute",
        top: currentPos.top,
        left: currentPos.left
    })

So if “wordglow3” I want to add “hit.play()” and for “wordglow” I want to add “miss.play()”

  • 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-13T11:46:59+00:00Added an answer on June 13, 2026 at 11:46 am

    OK, first the obvious solution with if/else-statements:

    var c = b.clone();
    if ( b.data("letter") == target.data("letter") ) {
        c.addClass("wordglow3");
        hit.play();
    } else {
        c.addClass("wordglow");
        miss.play();
    }
    c.appendTo(table).css(…);
    

    Now, we could remove some duplicate code using variables:

    var className, sound;
    if ( b.data("letter") == target.data("letter") ) {
        className = "wordglow3";
        sound = hit;
    } else {
        className = "wordglow";
        sound = miss;
    }
    b.clone().addClass(className).appendTo(table).css(…);
    sound.play();
    

    or even shorter by initialising them with the defaults:

    var className, = "wordglow",
        sound = miss;
    if ( b.data("letter") == target.data("letter") ) {
        className += "";
        sound = hit;
    }
    …
    

    Using the ternary operator gets harder. We could use the comma operator to chain different actions in the same expression:

    b.clone().addClass( b.data("letter") == target.data("letter")
      ? hit.play(), "wordglow3"
      : miss.play(), "wordglow"
    ).appendTo(table).css(…);
    

    But this is ugly. A better choice would be using a variable for the condition and two ternary operators:

    var success = b.data("letter") == target.data("letter");
    b.clone().addClass(success ? "wordglow3" : "wordglow").appendTo(table).css(…);
    (success ? hit : miss).play();
    

    Once we’ve got here, you even might consider an extra data structure for your sounds and class names, to replace millions of (especially nested) if-statements with selection by key:

    // global:
    var sounds = { // or use an arrays
        "0": …, // miss
        "1": …  // hit
    };
    var classes = {
        "0": "wordglow",
        "1": "wordglow3"
    };
    // […]
    // then, select values by casting the boolean to numeric keys:
    var success = b.data("letter") == target.data("letter");
    b.clone().addClass(classes[+success]).appendTo(table).css(…);
    sounds[+success].play();
    

    This makes extending your application with other cases easy, and allows an easier maintenance of used class names or sounds (in a central place) if they are used like this everywhere. Also, we’ve reduced two variables hit and miss to only one sounds.

    Decide yourself which code snippet is best readable or most appropriate in your situation.

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

Sidebar

Related Questions

Currently I have a loop that tries to find an unused filename by adding
As part of a course assignment, we have been tasked with adding an extra
I have a ComboBox, bound to a DataTable. I'm trying to add an extra
I'm currently trying to find a way of adding some extra elements to existing
While adding extra functionality to the main view I have in my application, I've
I'm currently adding a div to use as a slider programmatically, but when I
I am currently adding products programatically to the cart, to create a 'free sample'
I'm currently adding transposition tables in my chess engine, and I'm having issues with
I currently am adding some features to our logging-library. One of these is the
I'm adding a toolbar to my application and currently I'm adding some toggle buttons

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.