Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution(name){
- let answer = -1;
- let idx = 0;
- const newArr = [];
- if(/[A]+/.test(name)) {
- const match = name.match(/[A]+/g);
- let max = 0;
- for(let i = 0; i < match.length; i++) {
- if(match[i].length > max) {
- max = match[i].length;
- if(name.indexOf(match[i]) - 2 < max){
- idx = name.indexOf(match[i]);
- }
- }
- }
- } else idx = name.length - 1;
- for(let i = 0; i < idx; i++) {
- newArr.push(name[i]);
- }
- for(let j = name.length - 1; j >= idx; j--) {
- newArr.push(name[j]);
- }
- for(let k = 0; k < newArr.length; k++) {
- if(newArr[k] !== “A”) {
- const strNum = newArr[k].charCodeAt() - 65;
- strNum > 13 ? answer += (26 - strNum) : answer += strNum;
- answer += 1;
- } else {
- if(newArr[k+1] !== “A” && newArr[k+1]) {
- answer += 1;
- }
- }
- }
- return answer;
- }
Add Comment
Please, Sign In to add comment