How to split a string with consecutive uppercase and lowercase letters (to get the number atoms of a...
I am trying to play with an algorithm where it requires me to read the number of atoms contained in an array, to which I have only began to test out the possibilities and to do so I am checking the uppercase letters and lowercase letters separately and trying to join them together afterwards, here's how I was trying:
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
Now i successfully managed to console them the way I wanted.
Now the algorithm requires me to print them out exactly like this:
Cl1Na1
, but I can't seem to join the arrays, all that happens is they turn out to be like N1a
in one line and C1l
in the other, please help.
javascript algorithm
|
show 1 more comment
I am trying to play with an algorithm where it requires me to read the number of atoms contained in an array, to which I have only began to test out the possibilities and to do so I am checking the uppercase letters and lowercase letters separately and trying to join them together afterwards, here's how I was trying:
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
Now i successfully managed to console them the way I wanted.
Now the algorithm requires me to print them out exactly like this:
Cl1Na1
, but I can't seem to join the arrays, all that happens is they turn out to be like N1a
in one line and C1l
in the other, please help.
javascript algorithm
1
What are you passing into the function? What is the definition ofmolecule
?
– mwilson
Jan 18 at 20:27
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
1
So, given the input ofNaCl
, what are you expecting the outcome to be?
– mwilson
Jan 18 at 20:37
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Can the input be likeH2O
andH2SO4
?
– adiga
Jan 18 at 20:44
|
show 1 more comment
I am trying to play with an algorithm where it requires me to read the number of atoms contained in an array, to which I have only began to test out the possibilities and to do so I am checking the uppercase letters and lowercase letters separately and trying to join them together afterwards, here's how I was trying:
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
Now i successfully managed to console them the way I wanted.
Now the algorithm requires me to print them out exactly like this:
Cl1Na1
, but I can't seem to join the arrays, all that happens is they turn out to be like N1a
in one line and C1l
in the other, please help.
javascript algorithm
I am trying to play with an algorithm where it requires me to read the number of atoms contained in an array, to which I have only began to test out the possibilities and to do so I am checking the uppercase letters and lowercase letters separately and trying to join them together afterwards, here's how I was trying:
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
Now i successfully managed to console them the way I wanted.
Now the algorithm requires me to print them out exactly like this:
Cl1Na1
, but I can't seem to join the arrays, all that happens is they turn out to be like N1a
in one line and C1l
in the other, please help.
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
function countAtoms(molecule) {
var element = molecule;
var newArray = ;
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray = uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray = lowercase;
console.log(newArray)
}
}
}
javascript algorithm
javascript algorithm
edited yesterday
adiga
7,02262141
7,02262141
asked Jan 18 at 20:23
John RiversJohn Rivers
13913
13913
1
What are you passing into the function? What is the definition ofmolecule
?
– mwilson
Jan 18 at 20:27
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
1
So, given the input ofNaCl
, what are you expecting the outcome to be?
– mwilson
Jan 18 at 20:37
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Can the input be likeH2O
andH2SO4
?
– adiga
Jan 18 at 20:44
|
show 1 more comment
1
What are you passing into the function? What is the definition ofmolecule
?
– mwilson
Jan 18 at 20:27
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
1
So, given the input ofNaCl
, what are you expecting the outcome to be?
– mwilson
Jan 18 at 20:37
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Can the input be likeH2O
andH2SO4
?
– adiga
Jan 18 at 20:44
1
1
What are you passing into the function? What is the definition of
molecule
?– mwilson
Jan 18 at 20:27
What are you passing into the function? What is the definition of
molecule
?– mwilson
Jan 18 at 20:27
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
1
1
So, given the input of
NaCl
, what are you expecting the outcome to be?– mwilson
Jan 18 at 20:37
So, given the input of
NaCl
, what are you expecting the outcome to be?– mwilson
Jan 18 at 20:37
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Can the input be like
H2O
and H2SO4
?– adiga
Jan 18 at 20:44
Can the input be like
H2O
and H2SO4
?– adiga
Jan 18 at 20:44
|
show 1 more comment
4 Answers
4
active
oldest
votes
You need to split the string into groups that start with uppercase letters. One succinct way to do this is with a regular expression:
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
From there it looks like you want to map over them and add a 1
. This will give you an array that you can then join back together with join('')
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule likeH2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
add a comment |
You can use the below regex to split the items along with their atom count. This works even with molecules like H2SO4
, H2O
etc.
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
add a comment |
there you have, using reduce
and then looping over and object with all the sums :)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
add a comment |
To achieve expected result, use below option of making below changes to existing code
- newArray variable to empty string,
var newArray = '';
- Concat everytime instead of over writing newArray variable,
newArray += uppercase;
andnewArray += lowercase;
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
add a 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%2f54260944%2fhow-to-split-a-string-with-consecutive-uppercase-and-lowercase-letters-to-get-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to split the string into groups that start with uppercase letters. One succinct way to do this is with a regular expression:
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
From there it looks like you want to map over them and add a 1
. This will give you an array that you can then join back together with join('')
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule likeH2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
add a comment |
You need to split the string into groups that start with uppercase letters. One succinct way to do this is with a regular expression:
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
From there it looks like you want to map over them and add a 1
. This will give you an array that you can then join back together with join('')
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule likeH2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
add a comment |
You need to split the string into groups that start with uppercase letters. One succinct way to do this is with a regular expression:
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
From there it looks like you want to map over them and add a 1
. This will give you an array that you can then join back together with join('')
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
You need to split the string into groups that start with uppercase letters. One succinct way to do this is with a regular expression:
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
From there it looks like you want to map over them and add a 1
. This will give you an array that you can then join back together with join('')
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
let molecule = "NaCl"
let arr = molecule.match(/([A-Z][a-z]*)/g) // match uppercase letter followed by 0 or more lower case letters
console.log(arr)
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
let m = "NaCl"
function countAtoms(molecule) {
var element = molecule.match(/([A-Z][a-z]*)/g)
return element.map(el => el+1).join('')
}
console.log(countAtoms(m))
answered Jan 18 at 20:40
Mark MeyerMark Meyer
38.1k33159
38.1k33159
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule likeH2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
add a comment |
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule likeH2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
yep, it looks exactly like what i was aiming for
– John Rivers
Jan 18 at 21:16
This doesn't appear to handle a molecule like
H2SO4
– Ian McLaird
Jan 18 at 21:42
This doesn't appear to handle a molecule like
H2SO4
– Ian McLaird
Jan 18 at 21:42
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like
['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
That's not part of the question (or it wasn't when I wrote the answer) @IanMcLaird. It looks like the OP is doing this one step at a time. For all we know the "atoms contained in an array" may be like
['H', 'H', 'S', 'O', 'O', 'O', 'O']
– Mark Meyer
Jan 18 at 21:43
add a comment |
You can use the below regex to split the items along with their atom count. This works even with molecules like H2SO4
, H2O
etc.
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
add a comment |
You can use the below regex to split the items along with their atom count. This works even with molecules like H2SO4
, H2O
etc.
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
add a comment |
You can use the below regex to split the items along with their atom count. This works even with molecules like H2SO4
, H2O
etc.
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
You can use the below regex to split the items along with their atom count. This works even with molecules like H2SO4
, H2O
etc.
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
const molecule = "H2SO4",
arr = molecule.match(/[A-Z][a-z]*d?/g),
mappedArray = arr.map(a => /d/g.test(a) ? a : a+1),
joinedString = mappedArray.join('');
console.log(mappedArray)
console.log(joinedString)
edited Jan 18 at 21:04
answered Jan 18 at 20:55
adigaadiga
7,02262141
7,02262141
add a comment |
add a comment |
there you have, using reduce
and then looping over and object with all the sums :)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
add a comment |
there you have, using reduce
and then looping over and object with all the sums :)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
add a comment |
there you have, using reduce
and then looping over and object with all the sums :)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
there you have, using reduce
and then looping over and object with all the sums :)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
function countAtoms(molecule) {
console.log('Hello World');
const sums = molecule.reduce((sum, atomKey) => {
const atom = atomKey.toUpperCase()
if (!sum[atom]) sum[atom] = 0;
sum[atom] += 1;
return sum;
}, {})
let finalString = '';
Object.keys(sums).forEach(sumKey => {
finalString += sumKey + sums[sumKey];
})
return finalString;
}
const result = countAtoms(['NA', 'CA', 'H', 'H', 'NA', 'na']);
console.log(result)
answered Jan 18 at 20:41
Prince HernandezPrince Hernandez
1,2621313
1,2621313
add a comment |
add a comment |
To achieve expected result, use below option of making below changes to existing code
- newArray variable to empty string,
var newArray = '';
- Concat everytime instead of over writing newArray variable,
newArray += uppercase;
andnewArray += lowercase;
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
add a comment |
To achieve expected result, use below option of making below changes to existing code
- newArray variable to empty string,
var newArray = '';
- Concat everytime instead of over writing newArray variable,
newArray += uppercase;
andnewArray += lowercase;
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
add a comment |
To achieve expected result, use below option of making below changes to existing code
- newArray variable to empty string,
var newArray = '';
- Concat everytime instead of over writing newArray variable,
newArray += uppercase;
andnewArray += lowercase;
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
To achieve expected result, use below option of making below changes to existing code
- newArray variable to empty string,
var newArray = '';
- Concat everytime instead of over writing newArray variable,
newArray += uppercase;
andnewArray += lowercase;
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
function countAtoms(molecule) {
var element = molecule;
var newArray = '';
console.log('Hello World');
for (let i = 0; i < element.length; i++) {
if(element[i] == element[i].toUpperCase()){
var uppercase = element[i]+'1';
newArray += uppercase;
console.log(newArray);
}
else{
var lowercase = element[i];
newArray += lowercase;
console.log(newArray)
}
}
}
countAtoms('NaCl')
answered Jan 18 at 20:43
Naga Sai ANaga Sai A
5,6701825
5,6701825
add a comment |
add a 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%2f54260944%2fhow-to-split-a-string-with-consecutive-uppercase-and-lowercase-letters-to-get-t%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
What are you passing into the function? What is the definition of
molecule
?– mwilson
Jan 18 at 20:27
im passing exactly like this: countAtoms('NaCl'); the definition of molecule is literal, i would say. The algorithm requires me to verify how many molecules a certain chemical element passed as argument has and return the result, and i was trying to concat them doing var res = uppercase.concat(lowercase) hence the result getting printed as described above
– John Rivers
Jan 18 at 20:32
1
So, given the input of
NaCl
, what are you expecting the outcome to be?– mwilson
Jan 18 at 20:37
Im trying to figure out how to join the strings, as i was saying for now i managed to identify the amount of molecules in each element in this case, which is 1 Na and 1 Cl, what i wanna do now is not only make it Na1Cl1, but i also want to reverse it to Cl1Na1
– John Rivers
Jan 18 at 20:39
Can the input be like
H2O
andH2SO4
?– adiga
Jan 18 at 20:44