0

I have a single input where I can submit text as such: name, score

I'm parsing the results so that the name is stored in a variable as a string and the score is stored in a variable as an integer. This looks quite clunky. Is there a way to parse the text without requiring five separate variables?

// capture submitted string result
var namescore = document.getElementById('namescore').value;

// split it at the comma
var parts = namescore.split(", ");

// make sure first part is a string
var pname = parts [0].toString();

// convert score string to integer
var scoreString = parts [1];
var score = parseInt(scoreString, 10);
1

4 Answers 4

5

Here, two variables:

var parts = document.getElementById('namescore').value.split(", ");
var result = {
    name: parts[0],
    score: parseInt(parts[1], 10)
}

You can access the score like that:

result.score

And the name:

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

4 Comments

+"" is as pointless as .toString() was in the original code.
maybe, but it seems like a more JavaScript way for converting to String... Which is not needed here anyway, since split returns Strings in the first place. Oh well.
The value of an element is always a string.
As many have noted, there is need for converting to String, so I edited the answer to reflect that.
2

Two could be eliminated easily even in your code:

// split it at the comma
var parts = document.getElementById('namescore').value.split(", ");

// make sure first part is a string
var pname = parts[0].toString();

// convert score string to integer
var score = parseInt(parts[1], 10);

Or you don't really need pname and score, if you only use them once, then you can use what's on the right side of the equation there. Then you're left only with 1 variable: parts

1 Comment

Someone appears to be quick to downvote a lot of correct answers :O
1

You do not, by any means, need to introduce a variable for every operation in that manner:

// capture submitted string result
var parts = document.getElementById('namescore').split(", ");

// make sure first part is a string
var pname = parts[0].toString();

// convert score string to integer
var score = parseInt(parts[1], 10);

The above code does the exact same things as your code. You could skip the .toString call: the result of splitting a string will always be an array of strings.

Comments

0

Any reason not to leave them as members of the parts array?

// capture submitted string result
var namescore = document.getElementById('namescore').value;

// split it at the comma
var parts = namescore.split(", ");


parts[0] = parts[0].toString();
parts[1] = parseInt( parts[1], 10 );

4 Comments

That toString is probably unnecessary. What else would it be?
@Mathletics - Agreed, it's the OP's, not mine :)
Sure, but as the question is about reducing the size of the code... ;)
I want to give them more human readable names and the rest of the code refers to the "pname" and "score" variables, so I'd like to store the results in those variables.

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.