Changing String To Number And Summing Up Digits
Solution 1:
You can avoid storing "letter":"number"
mapping, by taking advantage of Ascii values, just convert uppercase and subtract 64
functionm_o() {
var value ="SCORE";
var finalCount = 0;
for (var i = 0; i < value.length; i++) {
finalCount += (value.toUpperCase().charCodeAt(i) - 64);
}
$("#output").html(finalCount);
}
click for Fiddle Demo
Solution 2:
You get 1
as an output because of this parseInt
's behavior:
If
parseInt
encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point.
What I think you need is to use eval
and use a +
before each replacement:
Replace return r_o[matched];
with return "+"+r_o[matched];
and use:
value = parseInt(eval(value), 10);
Or if you are not considering eval
, here is a custom function:
functionsumup(s){
var total= 0, s= s.match(/\+\d+/g) || [];
while(s.length){
total+= parseInt(s.shift());
}
return total;
}
Here is your updated snippet:
// My globalsvar output = $("#output");
functionsumup(s){
var total= 0, s= s.match(/\+\d+/g) || [];
while(s.length){
total+= parseInt(s.shift());
}
return total;
}
functionm_o() {
// var input = $("#input");// var value = input.val();var value = "SCORE";
// Settingvar r_o = {
// Setting #customizingA: "1",
B: "2",
C: "3",
D: "4",
E: "5",
F: "6",
G: "7",
H: "8",
I: "9",
J: "10",
K: "11",
L: "12",
M: "13",
N: "14",
O: "15",
P: "16",
Q: "17",
R: "18",
S: "19",
T: "20",
U: "21",
V: "22",
W: "23",
X: "24",
Y: "25",
Z: "26",
};
// Translatingvar re = newRegExp(Object.keys(r_o).join("|"), "g");
value = value.replace(re, function (matched) {
return"+"+r_o[matched];
});
value = sumup(value);
output.val(value);
}
<!doctype html><html><head><title>5X Script</title></head><body><!--<textarea id="input">
</textarea>--><inputtype="button"value="Translate"onclick="m_o()"/><textareaid="output"></textarea></body><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script></html>
Solution 3:
I feel like you are overcomplicating it. You could just go over each character in the string using a loop.
For each letter, check that it exists in the r_o
object, and if so, get the corresponding value from the object and add it to the total.
Here is an example
// Translatingvar length = value.length; //how many characters in the string?var total = 0; //what you will returnfor(let i=0; i < value.length; i++) //for each character in the string
{
var m = value[i];
if(r_o.hasOwnProperty(m)) total += r_o[m];
//if the char is in the obj, add the corresponding value
}
output.val(total); //return the new total
Solution 4:
What you want is like this
var value = "SCORE";
// Settingvar r_o = {
// Setting #customizingA: "1",
B: "2",
C: "3",
D: "4",
E: "5",
F: "6",
G: "7",
H: "8",
I: "9",
J: "10",
K: "11",
L: "12",
M: "13",
N: "14",
O: "15",
P: "16",
Q: "17",
R: "18",
S: "19",
T: "20",
U: "21",
V: "22",
W: "23",
X: "24",
Y: "25",
Z: "26",
};
// Translatingvar re = newRegExp(Object.keys(r_o).join("|"), "g");
var output =0;
value.replace(re, function (matched) {
output += parseInt(r_o[matched]);
});
alert(output);
Solution 5:
As far as I know, parseInt cannot evaluate expressions like you are doing. Anyways, I would suggest to use String.prototype.charCodeAt
in order to make your code more readable and changing your functions and variables names.
EDIT: Just to make the answer more clear: what your are passing to parseInt
is actually a string that contains an expression like "1 + 2 + 3 + 4
"...
What parseInt
is trying to do is just parse the first integer it gets, in this case a 1
. As the other told you, your solution seem really complicate and you should change it, but in case you are really happy with this solution (you shouldn't) you could change:
value = parseInt(value.split("").join(" + "), 10);
with:
eval("value = " + value.split("").join(" + ") + ";");
There's actually another mistake in your code: you are doing a split("")
. This can lead to an error when you replace a character with a 2 digit number, like 19 (this is why even with my solution, you'll get 33 instead of 60). Your RegEx replaces S
with 19
, but then, when you split and join, your 2 digits number is split in 1 and 9. A simple solution could be to add a whitespace when replacing and then using it to split the values, replacing:
return r_o[matched];
with
return' ' + r_o[matched];
and
eval("value = " + value.split("").join(" + ") + ";");
with
eval("value = " + value.split(" ").join(" + ") + ";");
Little explanation: Right now when you do your replacement you are actually replacing a character with <space><number>
. You then use this space to split correctly the values and then joining them with the +
operator. Finally, you evaluate the string "value = 1 + 2 + 3 + 4;
.
This answer is just for an "educational" purpose, your code should be rewritten
Working snippet:
// My globalsvar output = $("#output");
functionm_o() {
// var input = $("#input");// var value = input.val();var value = "SCORE";
// Settingvar r_o = {
// Setting #customizingA: "1",
B: "2",
C: "3",
D: "4",
E: "5",
F: "6",
G: "7",
H: "8",
I: "9",
J: "10",
K: "11",
L: "12",
M: "13",
N: "14",
O: "15",
P: "16",
Q: "17",
R: "18",
S: "19",
T: "20",
U: "21",
V: "22",
W: "23",
X: "24",
Y: "25",
Z: "26",
};
// Translatingvar re = newRegExp(Object.keys(r_o).join("|"), "g");
value = value.replace(re, function (matched) {
return' ' + r_o[matched];
});
eval("value = " + value.split(' ').join(" + ") + ";");
output.val(value);
}
<!doctype html><html><head><title>5X Script</title></head><body><!--<textarea id="input">
</textarea>--><inputtype="button"value="Translate"onclick="m_o()"/><textareaid="output"></textarea></body><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script></html>
Post a Comment for "Changing String To Number And Summing Up Digits"