base -2

by Igor Cuckovic

JavaScript

function solution (M) {
    var resultArr = [];
    var tempVal, remainder;
    var base = -2;
    
    while (M !== 0) {
        //console.log(M)
        tempVal = M;
        M = Math.ceil(M / base);
        remainder = tempVal % base;
        
        
        if (remainder < 0) {
            remainder += Math.abs(base);
        }
        
        remainder = Math.abs(remainder);   
        
        resultArr.push(remainder);
        
    }
    
    return resultArr;
}

console.log(solution(42));