0

I'm just starting programming, can I make this code shorter? Maybe with an array? Thanks.

function createDragName1(){
    var firstName;
    var random1 = Math.floor((Math.random()*17)+0);
    var name1 = "va";
    var name2 = "tuul";

    switch(random1){
        case 0:
            firstName = name1;
            break;
        case 1:
            firstName = name2;
            break;
        default:
            firstName = "Error";
    }

    var testingName = firstName.substring(0,1).toUpperCase()+firstName.substring(1,6);

    return(testingName);

}
7
  • 1
    write an unit test and then try to refactor it Commented Aug 22, 2013 at 2:42
  • 4
    Why are you multiplying by 17? Do you really want to return "Error" over 88% of the time? Commented Aug 22, 2013 at 2:43
  • It's not terribly long. Why do you need to make it shorter? Commented Aug 22, 2013 at 2:44
  • Let me count for you: +0, 4 extra var statements, unnecessary temporary variable, parentheses around return, Math.random, default switch case that could be coded with a default value, yes, you can make it shorter. Commented Aug 22, 2013 at 2:46
  • 1
    This isn't code review...? Commented Aug 22, 2013 at 2:47

4 Answers 4

1

Sure, you can use a map:

function createDragName1(){
    var random1 = Math.floor(Math.random() * 17), // [0, 17)
    names = ['Va', 'Tuul']; // title cased names

    return names[random1] || 'Error';
}

The expression names[random1] || 'Error' returns a name if 0 <= random1 < names.length and 'Error' otherwise.

I've also removed the title case logic by manually title casing names.

Btw, the random number will in most cases be outside of the desired range, so you will return Error an awful many times.

Sign up to request clarification or add additional context in comments.

2 Comments

Put the array inline in the return statement and it's even shorter ;)
@plalx Doing that would degrade the code readability though :)
0
function createDragName1(){

    var random1 = Math.floor((Math.random()*17)+0);
    var firstName = ["va","tuul"][random1];
    if(!firstName){
       firstName = 'Error';
    }

    var testingName = firstName.substring(0,1).toUpperCase()+firstName.substr(1);

    return(testingName);

}

Comments

0

Assuming you want to return either "Va" or "Tuul" with equal probability, and don't really want to return "Error":

function createDragName1() {
    return Math.random() < 0.5 ? "Va" : "Tuul";
}

However, if you do want the original behavior:

function createDragName1() {
    var rand = 17 * Math.random();
    return rand < 1 ? "Va" : rand < 2 ? "Tuul" : "Error";
}

A general method for picking a random name out of an array is:

var array = ["Va", "Tuul"];
var randomElement = array[0 | (array.length * Math.random())];

(The weird notation 0 | x evaluates to the integer part of x because the bitwise | operator converts arguments to integer if necessary.) If you want an expanded range:

var array = ["Va", "Tuul"];
var randomElementOrError = array[0 | (17 * Math.random())] || "Error";

Comments

0

I don't think you can make it much shorter than:

function createDragName1() {
    return ['Va', 'Tuul'][Math.floor(Math.random() * 17)] || 'Error';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.