추가 0을 계속 인쇄하는 이유를 알아낼 수없는 것 같습니다.
현재 값이 730이면 다음과 같이 표시됩니다.
Expected: '700 + 30', instead got: '700 + 30 + 0'
기준 :
"번호가 주어지며 확장 양식의 문자열로 반환해야합니다. 예 :
12 10 + 2를 반환해야합니다.
42 40 + 2를 반환해야 함
70304는 70000 + 300 + 4를 반환해야합니다.
참고 : 모든 숫자는 0보다 큰 정수입니다. "
function expandedForm(num) {
var i,
position,
numArr = Array.from(num.toString()).map(Number),
numArrLen = numArr.length,
result = '';
if(num < 10){
return num;
} else {
for(i = 0; i < numArrLen; i++){
position = numArrLen-1-i;
if( i === numArrLen-1 ){
result += numArr[i] * Math.pow(10, position);
console.log('done',result);
} else {
if(numArr[i] !== 0){
result += numArr[i] * Math.pow(10, position) + " + ";
console.log('keep going',result);
} else {
continue;
console.log('zero', result);
}
}
}
return result;
}
}
0을 설명하지 않는 수표가 첫 번째로 들어가므로 해당 수표에서 0인지 확인해야합니다.
if (i === numArrLen-1 && numArr[i]!==0)
그리고 그 후에 당신이 가질 문제는 당신이 후행을 가질 것 +
입니다.
내가 할 일은 문자열을 추가하는 대신 배열에 푸시하고 조인하는 것입니다.
var result = []
result.push(30)
result.push(2)
console.log(result.join(" + ")
배열 메서드를 사용하여 실제로 해결할 수 있습니다.
(102030).toString().split("").map((n,i,a) => n*Math.pow(10, a.length-i-1)).filter(n => n>0).join(" + ")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다