How can I sum all numbers 1 from a string?
I need to sum all numbers 1 from a string!
For example: "00110010" = 1+1+1 = 3...
psum will receive this result and then I will check
if(psum >= 3)
return person;
I need to find a way to solve it in javascript ES6 but I can't use any for, while or forEach loop, unfortunately!!!
Could you help me?
javascript string int
add a comment |
I need to sum all numbers 1 from a string!
For example: "00110010" = 1+1+1 = 3...
psum will receive this result and then I will check
if(psum >= 3)
return person;
I need to find a way to solve it in javascript ES6 but I can't use any for, while or forEach loop, unfortunately!!!
Could you help me?
javascript string int
1
Is this an assignment? Why can't you use those constructs? You don't have many options left exceptreduce. That's probably the point of the assignment.
– Carcigenicate
Nov 15 '18 at 20:13
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
1
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20
add a comment |
I need to sum all numbers 1 from a string!
For example: "00110010" = 1+1+1 = 3...
psum will receive this result and then I will check
if(psum >= 3)
return person;
I need to find a way to solve it in javascript ES6 but I can't use any for, while or forEach loop, unfortunately!!!
Could you help me?
javascript string int
I need to sum all numbers 1 from a string!
For example: "00110010" = 1+1+1 = 3...
psum will receive this result and then I will check
if(psum >= 3)
return person;
I need to find a way to solve it in javascript ES6 but I can't use any for, while or forEach loop, unfortunately!!!
Could you help me?
javascript string int
javascript string int
asked Nov 15 '18 at 20:10
Riller VincciRiller Vincci
84
84
1
Is this an assignment? Why can't you use those constructs? You don't have many options left exceptreduce. That's probably the point of the assignment.
– Carcigenicate
Nov 15 '18 at 20:13
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
1
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20
add a comment |
1
Is this an assignment? Why can't you use those constructs? You don't have many options left exceptreduce. That's probably the point of the assignment.
– Carcigenicate
Nov 15 '18 at 20:13
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
1
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20
1
1
Is this an assignment? Why can't you use those constructs? You don't have many options left except
reduce. That's probably the point of the assignment.– Carcigenicate
Nov 15 '18 at 20:13
Is this an assignment? Why can't you use those constructs? You don't have many options left except
reduce. That's probably the point of the assignment.– Carcigenicate
Nov 15 '18 at 20:13
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
1
1
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20
add a comment |
3 Answers
3
active
oldest
votes
You need to use the reduce() method.
let input = '00110010'
let array = input.split("").map(x => parseInt(x));
let sum = array.reduce((acc, val) =>
return acc + val;
);
console.log(sum)
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
add a comment |
In one statement:
let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);add a comment |
Note that summing the numbers 1 comes down to counting the numbers 1, which is what the following solutions do in different ways:
With match
You could use a regular expression /1/g:
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);match returns an array of substrings that match with the pattern 1. The / just delimit this regular expression, and the g means that all matches should be retrieved (global). The length of the returned array thus corresponds to the number of 1s in the input. If there are no matches at all, then match will return null, so that does not have a .length property. To take care of that || will check for that null (which is falsy in a boolean expression) and so will be taken instead of null.
With replace
This is a similar principle, but by matching non-1 characters and removing them:
var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);[^1] means: a character that is not 1. replace will replace all matches with the second argument (empty string), which comes down to returning all characters that do not match. This is like a double negative: return characters that do not match with not 1. So you get only the 1s :-) .length will count those.
With split:
var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);split splits the string into an array of substrings that do not have the given substring ("1"). So even if there are no "1" at all, you get one such substring (the whole string). This means that by getting the length, we should reduce it by 1 to get the number of 1s.
With a recursive function:
var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);Here the function count1 is introduced. It first checks if the given string p is empty. If so, length is zero, and that is returned. If not empty, the first character is compared with 1. This can be false or true. This result is converted to 0 or 1 respectively and added to a recursive call result. That recursive call counts the number 1s in the rest of the input (excluding the first character in which the 1s were already counted).
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327199%2fhow-can-i-sum-all-numbers-1-from-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use the reduce() method.
let input = '00110010'
let array = input.split("").map(x => parseInt(x));
let sum = array.reduce((acc, val) =>
return acc + val;
);
console.log(sum)
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
add a comment |
You need to use the reduce() method.
let input = '00110010'
let array = input.split("").map(x => parseInt(x));
let sum = array.reduce((acc, val) =>
return acc + val;
);
console.log(sum)
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
add a comment |
You need to use the reduce() method.
let input = '00110010'
let array = input.split("").map(x => parseInt(x));
let sum = array.reduce((acc, val) =>
return acc + val;
);
console.log(sum)
You need to use the reduce() method.
let input = '00110010'
let array = input.split("").map(x => parseInt(x));
let sum = array.reduce((acc, val) =>
return acc + val;
);
console.log(sum)
answered Nov 15 '18 at 20:20
Beth ShookBeth Shook
63
63
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
add a comment |
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Very good Beth! It worked as well
– Riller Vincci
Nov 15 '18 at 20:30
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Be aware that if your string has 2 or 3, that those numbers are summed up as well. From the question I understood only numbers 1 should be added.
– trincot
Nov 15 '18 at 20:31
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
Ah, indeed @tricot. I may have misunderstood.
– Beth Shook
Nov 15 '18 at 20:33
add a comment |
In one statement:
let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);add a comment |
In one statement:
let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);add a comment |
In one statement:
let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);In one statement:
let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);let psum = "00110010".split('').reduce((t, n) => return t + parseInt(n), 0);
console.log(psum);answered Nov 15 '18 at 20:27
cybersamcybersam
40.6k53252
40.6k53252
add a comment |
add a comment |
Note that summing the numbers 1 comes down to counting the numbers 1, which is what the following solutions do in different ways:
With match
You could use a regular expression /1/g:
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);match returns an array of substrings that match with the pattern 1. The / just delimit this regular expression, and the g means that all matches should be retrieved (global). The length of the returned array thus corresponds to the number of 1s in the input. If there are no matches at all, then match will return null, so that does not have a .length property. To take care of that || will check for that null (which is falsy in a boolean expression) and so will be taken instead of null.
With replace
This is a similar principle, but by matching non-1 characters and removing them:
var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);[^1] means: a character that is not 1. replace will replace all matches with the second argument (empty string), which comes down to returning all characters that do not match. This is like a double negative: return characters that do not match with not 1. So you get only the 1s :-) .length will count those.
With split:
var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);split splits the string into an array of substrings that do not have the given substring ("1"). So even if there are no "1" at all, you get one such substring (the whole string). This means that by getting the length, we should reduce it by 1 to get the number of 1s.
With a recursive function:
var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);Here the function count1 is introduced. It first checks if the given string p is empty. If so, length is zero, and that is returned. If not empty, the first character is compared with 1. This can be false or true. This result is converted to 0 or 1 respectively and added to a recursive call result. That recursive call counts the number 1s in the rest of the input (excluding the first character in which the 1s were already counted).
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
|
show 1 more comment
Note that summing the numbers 1 comes down to counting the numbers 1, which is what the following solutions do in different ways:
With match
You could use a regular expression /1/g:
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);match returns an array of substrings that match with the pattern 1. The / just delimit this regular expression, and the g means that all matches should be retrieved (global). The length of the returned array thus corresponds to the number of 1s in the input. If there are no matches at all, then match will return null, so that does not have a .length property. To take care of that || will check for that null (which is falsy in a boolean expression) and so will be taken instead of null.
With replace
This is a similar principle, but by matching non-1 characters and removing them:
var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);[^1] means: a character that is not 1. replace will replace all matches with the second argument (empty string), which comes down to returning all characters that do not match. This is like a double negative: return characters that do not match with not 1. So you get only the 1s :-) .length will count those.
With split:
var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);split splits the string into an array of substrings that do not have the given substring ("1"). So even if there are no "1" at all, you get one such substring (the whole string). This means that by getting the length, we should reduce it by 1 to get the number of 1s.
With a recursive function:
var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);Here the function count1 is introduced. It first checks if the given string p is empty. If so, length is zero, and that is returned. If not empty, the first character is compared with 1. This can be false or true. This result is converted to 0 or 1 respectively and added to a recursive call result. That recursive call counts the number 1s in the rest of the input (excluding the first character in which the 1s were already counted).
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
|
show 1 more comment
Note that summing the numbers 1 comes down to counting the numbers 1, which is what the following solutions do in different ways:
With match
You could use a regular expression /1/g:
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);match returns an array of substrings that match with the pattern 1. The / just delimit this regular expression, and the g means that all matches should be retrieved (global). The length of the returned array thus corresponds to the number of 1s in the input. If there are no matches at all, then match will return null, so that does not have a .length property. To take care of that || will check for that null (which is falsy in a boolean expression) and so will be taken instead of null.
With replace
This is a similar principle, but by matching non-1 characters and removing them:
var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);[^1] means: a character that is not 1. replace will replace all matches with the second argument (empty string), which comes down to returning all characters that do not match. This is like a double negative: return characters that do not match with not 1. So you get only the 1s :-) .length will count those.
With split:
var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);split splits the string into an array of substrings that do not have the given substring ("1"). So even if there are no "1" at all, you get one such substring (the whole string). This means that by getting the length, we should reduce it by 1 to get the number of 1s.
With a recursive function:
var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);Here the function count1 is introduced. It first checks if the given string p is empty. If so, length is zero, and that is returned. If not empty, the first character is compared with 1. This can be false or true. This result is converted to 0 or 1 respectively and added to a recursive call result. That recursive call counts the number 1s in the rest of the input (excluding the first character in which the 1s were already counted).
Note that summing the numbers 1 comes down to counting the numbers 1, which is what the following solutions do in different ways:
With match
You could use a regular expression /1/g:
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);match returns an array of substrings that match with the pattern 1. The / just delimit this regular expression, and the g means that all matches should be retrieved (global). The length of the returned array thus corresponds to the number of 1s in the input. If there are no matches at all, then match will return null, so that does not have a .length property. To take care of that || will check for that null (which is falsy in a boolean expression) and so will be taken instead of null.
With replace
This is a similar principle, but by matching non-1 characters and removing them:
var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);[^1] means: a character that is not 1. replace will replace all matches with the second argument (empty string), which comes down to returning all characters that do not match. This is like a double negative: return characters that do not match with not 1. So you get only the 1s :-) .length will count those.
With split:
var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);split splits the string into an array of substrings that do not have the given substring ("1"). So even if there are no "1" at all, you get one such substring (the whole string). This means that by getting the length, we should reduce it by 1 to get the number of 1s.
With a recursive function:
var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);Here the function count1 is introduced. It first checks if the given string p is empty. If so, length is zero, and that is returned. If not empty, the first character is compared with 1. This can be false or true. This result is converted to 0 or 1 respectively and added to a recursive call result. That recursive call counts the number 1s in the rest of the input (excluding the first character in which the 1s were already counted).
var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);var p = "00110010";
var psum = (p.match(/1/g) || ).length;
console.log(psum);var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);var p = "00110010";
var psum = p.replace(/[^1]/g, "").length;
console.log(psum);var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);var p = "00110010";
var psum = p.split("1").length - 1;
console.log(psum);var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);var p = "00110010";
var count1 = p => p.length && ((p[0] == "1") + count1(p.slice(1)));
var psum = count1(p);
console.log(psum);edited Nov 15 '18 at 20:57
answered Nov 15 '18 at 20:16
trincottrincot
130k1691125
130k1691125
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
|
show 1 more comment
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
Thank you @trincot
– Riller Vincci
Nov 15 '18 at 20:27
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
You're welcome. Let me know if you need clarification with one of the solutions.
– trincot
Nov 15 '18 at 20:28
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
It worked successfully.. I'm so grateful for your help!
– Riller Vincci
Nov 15 '18 at 20:29
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
Sure... I would like to understand! Let me know how it works
– Riller Vincci
Nov 15 '18 at 20:43
I added explanations.
– trincot
Nov 15 '18 at 20:57
I added explanations.
– trincot
Nov 15 '18 at 20:57
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327199%2fhow-can-i-sum-all-numbers-1-from-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is this an assignment? Why can't you use those constructs? You don't have many options left except
reduce. That's probably the point of the assignment.– Carcigenicate
Nov 15 '18 at 20:13
HAH @Carcigenicate it sure is. Too bad this is a question about javascript :/ How did I wander in here?
– Will
Nov 15 '18 at 20:16
What have you tried so far? This sounds like a class assignment with those restrictions.
– Herohtar
Nov 15 '18 at 20:16
1
Thank you @Will but I can't use a framework
– Riller Vincci
Nov 15 '18 at 20:17
I've tried to convert that string to int type, so I've got "110010", but what I need is the sum of number 1. I have no idea about what try again! @Herohtar
– Riller Vincci
Nov 15 '18 at 20:20