Given a string, turn each character into its ASCII character code and join them together to create a number - let's call this number total1:
Then replace any incidence of the number 7 with the number 1, and call this number 'total2':
Then return the difference between the sum of the digits in total1 and total2:
(요약) 인자로 받은 문자열 각 요소를 아스키코드로 변환하고, 7을 1로 바꾼 후, 바꾸기 전과 후의 모든 자리수를 더해서 차를 구하라.
function calc(x){ let charCodeStr = ''; let sevenCount = 0; for(let i = 0; i < x.length; i++) { let charCode = x[i].charCodeAt() + ''; charCodeStr += charCode; if(charCode.includes('7')) { for(let j = 0; j < charCode.length; j++) { charCode[j] === '7' && sevenCount++; } } } return sevenCount * 6; }
charCodeAt()
으로 각 문자열의 아스키코드를 구해서 문자열로 만들고,7
이 있으면 그 개수를sevenCount
에 카운트함.
7
을1
로 바꾼후 모든 자리 수 더한값의 차니까7
의 개수만큼6
을 곱해주면 됨.