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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:22:09+00:00 2026-05-31T07:22:09+00:00

I’m beginning my journey with JavaScript and programming in general. Not having many developers

  • 0

I’m beginning my journey with JavaScript and programming in general.
Not having many developers around I could ask how to do this or do that
i’m learning by doing however I’d like some pro’s to look around my code
and tell me how bad it is.

Any comments for a Noob will be appreciated. Thank you in advance!
I know there is probably zillion such classes but I want to learn from
scratch and not reuse someones code at this level of my knowledge.

this function returns colors in rgba(r,g,b,a) or rgb(r,g,b) format
taking a value for each channel or a random value if “random” is a parameter for
an color channel:

someColor = nutils.getNewColor(100,200,"random","random");

will return a string in format:

"rgba(100,232,0.12)" or "rgb(100,200,234)" if no alpha is passed

Here is the code I wrote:

window.utils.getNewColor = function (rRange, gRange, bRange, alpha) {
    function rValue() {
                    return Math.round(Math.random() * 255);
                }

                if (!rRange == undefined || rRange == null || typeof rRange === 'string' && rRange == "random") {
                    rRange = rValue();
                }
                if (!gRange == undefined || gRange == null || typeof gRange === 'string' && gRange == "random") {
                    gRange = rValue();
                }
                if (!bRange == undefined || bRange == null || typeof bRange === 'string' && bRange == "random") {
                    bRange = rValue()
                }
                if (typeof alpha === 'string' && alpha == "random") {
                    alpha = Math.round(Math.random() * 100) / 100
                    return "rgba(" + rRange + "," + gRange + "," + bRange + "," + alpha + ")";
                } else if (typeof alpha === 'number' && alpha < 1) {
                    return "rgba(" + rRange + "," + gRange + "," + bRange + "," + alpha + ")";
                }
                else {
                    return "rgb(" + rRange + "," + gRange + "," + bRange + ")";
                }
            };

UPDATE

After reading your comments I come up with a solution based on @KooilNic advise, however slightly modified to comprehend my lack of understanding ternary operations evaluation.

Here is the modified/updated code:

utils.getNewColor = function (rRange, gRange, bRange, alpha) {
    rRange = rRange || Math.round(Math.random() * 255);
    gRange = gRange || Math.round(Math.random() * 255);
    bRange = bRange || Math.round(Math.random() * 255);
    if (alpha === undefined) {
        return "rgb(" + [rRange, gRange, bRange].join(',') + ")";
    } else {
        alpha = alpha && alpha < 1 ? alpha : (Math.round(Math.random() * 100) / 100);
        return "rgba(" + [rRange, gRange, bRange, alpha].join(',') + ")";
    }
};

  • 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-31T07:22:10+00:00Added an answer on May 31, 2026 at 7:22 am

    Consider replacing rValue with a function to include those conditional checks, like so:

    function genValue(v) {
    
        if(v == undefined || v == null || v == "random") {
            return Math.round(Math.random() * 255);
        }
    
        return v;
    
    }
    
    // and later:
    rRange = genValue(rRange);
    gRange = genValue(gRange);
    

    That should reduce some code duplication.

    Also consider reducing the places where you construct your result ("rgb(" + ... + ")" to one or two places.

    EDIT:

    Some people are recommending code like !rValue || rValue == "random". Take care with this, as if you pass 0 in for rValue, you will get random colors.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.