Advertisement
_Lyuba_Ivanova_

Untitled

Nov 8th, 2019
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. function solve(input) {
  2. let sequences = input.split(/\s+/);
  3. let sum = 0;
  4. const isLowerCase = (string) => /^[a-z]*$/.test(string);
  5.  
  6. const isUpperCase = (string) => /^[A-Z]*$/.test(string);
  7.  
  8. for (let el of sequences) {
  9. let result = 0;
  10. let firstChar = el.charAt(0);
  11. let firstCharPosition = el.toLowerCase().charCodeAt(0) - 96;
  12. let lastChar = el.charAt(el.length - 1);
  13. let lastCharPosition = el.toLowerCase().charCodeAt(el.length - 1) - 96;
  14. let numberPart = Number(el.substring(1, el.length - 1));
  15.  
  16. if(isUpperCase(firstChar)){
  17. result = numberPart/firstCharPosition;
  18. sum += result;
  19. }
  20. if(isLowerCase(firstChar)){
  21. result = numberPart * firstCharPosition;
  22. sum += result;
  23. }
  24. if(isUpperCase(lastChar)){
  25. sum -= lastCharPosition;
  26. }
  27. if(isLowerCase(lastChar)){
  28. sum += lastCharPosition;
  29. }
  30. }
  31. console.log(sum.toFixed(2));
  32. }
  33. solve('A12b s17G');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement