Skip to main content
added 3092 characters in body
Source Link
George Reith
  • 2.5k
  • 1
  • 14
  • 16
var a = [
  'H', 1,       
  
 'He', 4,       
  
 'Li', 7,       
  
 'Be', 9,       
  
 'B', 11,      
  
 'C', 12,      
  
 'N', 14,      
  
 'O', 16,      
  
 'F', 19,      
  
 'Ne', 20,      
  
 'Na', 23,      
  
 'Mg', 24,      
  
 'Al', 27,      
  
 'Si', 28,      
  
 'P', 31,      
  
 'S', 32,      
  
 'Cl', 35,      
  
 'Ar', 40,      
  
 'K', 39,      
  
 'Ca', 40,      
  
 'Sc', 45,      
  
 'Ti', 48,      
  
 'V', 51,      
  
 'Cr', 52,      
  
 'Mn', 55,      
  
 'Fe', 56,      
  
 'Co', 59,      
  
 'Ni', 59,      
  
 'Cu', 64,      
  
 'Zn', 65,      
  
 'Ga', 70,      
  
 'Ge', 73,      
  
 'As', 75,      
  
 'Se', 79,      
  
 'Br', 80,      
  
 'Kr', 84,      
  
 'Rb', 85,      
  
 'Sr', 88,      
  
 'Y', 89,      
  
 'Zr', 91,      
  
 'Nb', 93,      
  
 'Mo', 96,      
  
 'Tc', 98,      
  
 'Ru', 101,     
  
 'Rh', 103,     
  
 'Pd', 106,     
  
 'Ag', 108,     
  
 'Cd', 112,     
  
 'In', 115,     
  
 'Sn', 119,     
  
 'Sb', 122,  
  
 'I', 127,        
  
 'Te', 128,     
  
 'Xe', 131,     
  
 'Cs', 133,     
  
 'Ba', 137,     
  
 'La', 139,     
  
 'Ce', 140,     
  
 'Pr', 141,     
  
 'Nd', 144,     
  
 'Pm', 145,     
  
 'Sm', 150,     
  
 'Eu', 152,     
  
 'Gd', 157,     
  
 'Tb', 159,     
  
 'Dy', 163,     
  
 'Ho', 165,     
  
 'Er', 167,     
  
 'Tm', 169,     
  
 'Yb', 173,     
  
 'Lu', 175,     
  
 'Hf', 178,     
  
 'Ta', 181,     
  
 'W', 184,     
  
 'Re', 186,     
  
 'Os', 190,     
  
 'Ir', 192,     
  
 'Pt', 195,     
  
 'Au', 197,     
  
 'Hg', 201,     
  
 'Tl', 204,     
  
 'Pb', 207,     
  
 'Bi', 209,     
  
 'Po', 209,     
  
 'At', 210,     
  
 'Rn', 222,     
  
 'Fr', 223,     
  
 'Ra', 226,     
  
 'Ac', 227,     
  
 'Pa', 231,
  'Th', 232,         
  
 'U', 238,     
  
 'Np', 237,     
  
 'Pu', 244,     
  
 'Am', 243,     
  
 'Cm', 247,     
  
 'Bk', 247,     
  
 'Cf', 251,     
  
 'Es', 252,     
  
 'Fm', 257
]; 

function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
    .split(RegExp(e+'[^ae + '[^a-z].*'))[0]
    .split('')
    .reduce(function(o, n) {
      return +n ? +n+o+n + o : n=='n == '-' ? --o : ++o;
    })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  $elementList.append("<li>"var resultString = "<li><b>" + symbol + "</b>";
  if (symbol.length === 1) resultString += "&nbsp;";
  resultString += " expected "<b>" + mass + "</b>";
  if (mass < 10) resultString += "&nbsp;";
  if (mass < 100) resultString += "&nbsp;";
  resultString += " got "<b>" + result + "</li>"b></li>";
  $elementList.append(resultString);
}
#elements li {
  font-family: "Consolas", monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>
var a = [
'H',1,        
 'He',4,        
 'Li',7,        
 'Be',9,        
 'B',11,       
 'C',12,       
 'N',14,       
 'O',16,       
 'F',19,       
 'Ne',20,       
 'Na',23,       
 'Mg',24,       
 'Al',27,       
 'Si',28,       
 'P',31,       
 'S',32,       
 'Cl',35,       
 'Ar',40,       
 'K',39,       
 'Ca',40,       
 'Sc',45,       
 'Ti',48,       
 'V',51,       
 'Cr',52,       
 'Mn',55,       
 'Fe',56,       
 'Co',59,       
 'Ni',59,       
 'Cu',64,       
 'Zn',65,       
 'Ga',70,       
 'Ge',73,       
 'As',75,       
 'Se',79,       
 'Br',80,       
 'Kr',84,       
 'Rb',85,       
 'Sr',88,       
 'Y',89,       
 'Zr',91,       
 'Nb',93,       
 'Mo',96,       
 'Tc',98,       
 'Ru',101,      
 'Rh',103,      
 'Pd',106,      
 'Ag',108,      
 'Cd',112,      
 'In',115,      
 'Sn',119,      
 'Sb',122,   
 'I',127,         
 'Te',128,      
 'Xe',131,      
 'Cs',133,      
 'Ba',137,      
 'La',139,      
 'Ce',140,      
 'Pr',141,      
 'Nd',144,      
 'Pm',145,      
 'Sm',150,      
 'Eu',152,      
 'Gd',157,      
 'Tb',159,      
 'Dy',163,      
 'Ho',165,      
 'Er',167,      
 'Tm',169,      
 'Yb',173,      
 'Lu',175,      
 'Hf',178,      
 'Ta',181,      
 'W',184,      
 'Re',186,      
 'Os',190,      
 'Ir',192,      
 'Pt',195,      
 'Au',197,      
 'Hg',201,      
 'Tl',204,      
 'Pb',207,      
 'Bi',209,      
 'Po',209,      
 'At',210,      
 'Rn',222,      
 'Fr',223,      
 'Ra',226,      
 'Ac',227,      
 'Pa',231,
'Th',232,          
 'U',238,      
 'Np',237,      
 'Pu',244,      
 'Am',243,      
 'Cm',247,      
 'Bk',247,      
 'Cf',251,      
 'Es',252,      
 'Fm',257
];
function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
  .split(RegExp(e+'[^a-z].*'))[0]
  .split('')
  .reduce(function(o,n) {
    return +n ? +n+o : n=='-' ? --o : ++o;
  })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  $elementList.append("<li>" + symbol + " expected " + mass + " got " + result + "</li>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>
var a = [
  'H', 1, 
  'He', 4, 
  'Li', 7, 
  'Be', 9, 
  'B', 11, 
  'C', 12, 
  'N', 14, 
  'O', 16, 
  'F', 19, 
  'Ne', 20, 
  'Na', 23, 
  'Mg', 24, 
  'Al', 27, 
  'Si', 28, 
  'P', 31, 
  'S', 32, 
  'Cl', 35, 
  'Ar', 40, 
  'K', 39, 
  'Ca', 40, 
  'Sc', 45, 
  'Ti', 48, 
  'V', 51, 
  'Cr', 52, 
  'Mn', 55, 
  'Fe', 56, 
  'Co', 59, 
  'Ni', 59, 
  'Cu', 64, 
  'Zn', 65, 
  'Ga', 70, 
  'Ge', 73, 
  'As', 75, 
  'Se', 79, 
  'Br', 80, 
  'Kr', 84, 
  'Rb', 85, 
  'Sr', 88, 
  'Y', 89, 
  'Zr', 91, 
  'Nb', 93, 
  'Mo', 96, 
  'Tc', 98, 
  'Ru', 101, 
  'Rh', 103, 
  'Pd', 106, 
  'Ag', 108, 
  'Cd', 112, 
  'In', 115, 
  'Sn', 119, 
  'Sb', 122, 
  'I', 127, 
  'Te', 128, 
  'Xe', 131, 
  'Cs', 133, 
  'Ba', 137, 
  'La', 139, 
  'Ce', 140, 
  'Pr', 141, 
  'Nd', 144, 
  'Pm', 145, 
  'Sm', 150, 
  'Eu', 152, 
  'Gd', 157, 
  'Tb', 159, 
  'Dy', 163, 
  'Ho', 165, 
  'Er', 167, 
  'Tm', 169, 
  'Yb', 173, 
  'Lu', 175, 
  'Hf', 178, 
  'Ta', 181, 
  'W', 184, 
  'Re', 186, 
  'Os', 190, 
  'Ir', 192, 
  'Pt', 195, 
  'Au', 197, 
  'Hg', 201, 
  'Tl', 204, 
  'Pb', 207, 
  'Bi', 209, 
  'Po', 209, 
  'At', 210, 
  'Rn', 222, 
  'Fr', 223, 
  'Ra', 226, 
  'Ac', 227, 
  'Pa', 231,
  'Th', 232, 
  'U', 238, 
  'Np', 237, 
  'Pu', 244, 
  'Am', 243, 
  'Cm', 247, 
  'Bk', 247, 
  'Cf', 251, 
  'Es', 252, 
  'Fm', 257
]; 

function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
    .split(RegExp(e + '[^a-z].*'))[0]
    .split('')
    .reduce(function(o, n) {
      return +n ? +n + o : n == '-' ? --o : ++o;
    })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  var resultString = "<li><b>" + symbol + "</b>";
  if (symbol.length === 1) resultString += "&nbsp;";
  resultString += " expected <b>" + mass + "</b>";
  if (mass < 10) resultString += "&nbsp;";
  if (mass < 100) resultString += "&nbsp;";
  resultString += " got <b>" + result + "</b></li>";
  $elementList.append(resultString);
}
#elements li {
  font-family: "Consolas", monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>
added 3092 characters in body
Source Link
George Reith
  • 2.5k
  • 1
  • 14
  • 16

Tests:

var a = [
'H',1,        
'He',4,        
'Li',7,        
'Be',9,        
'B',11,       
'C',12,       
'N',14,       
'O',16,       
'F',19,       
'Ne',20,       
'Na',23,       
'Mg',24,       
'Al',27,       
'Si',28,       
'P',31,       
'S',32,       
'Cl',35,       
'Ar',40,       
'K',39,       
'Ca',40,       
'Sc',45,       
'Ti',48,       
'V',51,       
'Cr',52,       
'Mn',55,       
'Fe',56,       
'Co',59,       
'Ni',59,       
'Cu',64,       
'Zn',65,       
'Ga',70,       
'Ge',73,       
'As',75,       
'Se',79,       
'Br',80,       
'Kr',84,       
'Rb',85,       
'Sr',88,       
'Y',89,       
'Zr',91,       
'Nb',93,       
'Mo',96,       
'Tc',98,       
'Ru',101,      
'Rh',103,      
'Pd',106,      
'Ag',108,      
'Cd',112,      
'In',115,      
'Sn',119,      
'Sb',122,   
'I',127,         
'Te',128,      
'Xe',131,      
'Cs',133,      
'Ba',137,      
'La',139,      
'Ce',140,      
'Pr',141,      
'Nd',144,      
'Pm',145,      
'Sm',150,      
'Eu',152,      
'Gd',157,      
'Tb',159,      
'Dy',163,      
'Ho',165,      
'Er',167,      
'Tm',169,      
'Yb',173,      
'Lu',175,      
'Hf',178,      
'Ta',181,      
'W',184,      
'Re',186,      
'Os',190,      
'Ir',192,      
'Pt',195,      
'Au',197,      
'Hg',201,      
'Tl',204,      
'Pb',207,      
'Bi',209,      
'Po',209,      
'At',210,      
'Rn',222,      
'Fr',223,      
'Ra',226,      
'Ac',227,      
'Pa',231,
'Th',232,          
'U',238,      
'Np',237,      
'Pu',244,      
'Am',243,      
'Cm',247,      
'Bk',247,      
'Cf',251,      
'Es',252,      
'Fm',257
];
function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
  .split(RegExp(e+'[^a-z].*'))[0]
  .split('')
  .reduce(function(o,n) {
    return +n ? +n+o : n=='-' ? --o : ++o;
  })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  $elementList.append("<li>" + symbol + " expected " + mass + " got " + result + "</li>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>

Tests:

var a = [
'H',1,        
'He',4,        
'Li',7,        
'Be',9,        
'B',11,       
'C',12,       
'N',14,       
'O',16,       
'F',19,       
'Ne',20,       
'Na',23,       
'Mg',24,       
'Al',27,       
'Si',28,       
'P',31,       
'S',32,       
'Cl',35,       
'Ar',40,       
'K',39,       
'Ca',40,       
'Sc',45,       
'Ti',48,       
'V',51,       
'Cr',52,       
'Mn',55,       
'Fe',56,       
'Co',59,       
'Ni',59,       
'Cu',64,       
'Zn',65,       
'Ga',70,       
'Ge',73,       
'As',75,       
'Se',79,       
'Br',80,       
'Kr',84,       
'Rb',85,       
'Sr',88,       
'Y',89,       
'Zr',91,       
'Nb',93,       
'Mo',96,       
'Tc',98,       
'Ru',101,      
'Rh',103,      
'Pd',106,      
'Ag',108,      
'Cd',112,      
'In',115,      
'Sn',119,      
'Sb',122,   
'I',127,         
'Te',128,      
'Xe',131,      
'Cs',133,      
'Ba',137,      
'La',139,      
'Ce',140,      
'Pr',141,      
'Nd',144,      
'Pm',145,      
'Sm',150,      
'Eu',152,      
'Gd',157,      
'Tb',159,      
'Dy',163,      
'Ho',165,      
'Er',167,      
'Tm',169,      
'Yb',173,      
'Lu',175,      
'Hf',178,      
'Ta',181,      
'W',184,      
'Re',186,      
'Os',190,      
'Ir',192,      
'Pt',195,      
'Au',197,      
'Hg',201,      
'Tl',204,      
'Pb',207,      
'Bi',209,      
'Po',209,      
'At',210,      
'Rn',222,      
'Fr',223,      
'Ra',226,      
'Ac',227,      
'Pa',231,
'Th',232,          
'U',238,      
'Np',237,      
'Pu',244,      
'Am',243,      
'Cm',247,      
'Bk',247,      
'Cf',251,      
'Es',252,      
'Fm',257
];
function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
  .split(RegExp(e+'[^a-z].*'))[0]
  .split('')
  .reduce(function(o,n) {
    return +n ? +n+o : n=='-' ? --o : ++o;
  })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  $elementList.append("<li>" + symbol + " expected " + mass + " got " + result + "</li>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>

Source Link
George Reith
  • 2.5k
  • 1
  • 14
  • 16

#Javascript ES6 354 bytes

p=e=>+'1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm'.split(RegExp(e+'[^a-z].*'))[0].split('').reduce((o,n)=>+n?+n+o:n=='-'?--o:++o)

Ungolfed:

function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
  .split(RegExp(e+'[^a-z].*'))[0]
  .split('')
  .reduce(function(o,n) {
    return +n ? +n+o : n=='-' ? --o : ++o;
  })
}

The elements are stored in a string of numbers symbols and minus characters. The mass of any given element is the number of letter characters preceding it, plus the sum of digits subtracted by the occurrences of -.