Ad
  • Custom User Avatar

    why does my solution always return 0?
    registers are declared as global variables. Should I implement readReg / writeReg methods in my solution?

    var reg = {'a':0,'b':0,'c':0,'d':0};
    var stack = [];

    function Machine(cpu) {

    function exec(inst) {
    console.log(inst);
    const getArg = (arg) => /[0-9]+/.test(arg)? +arg : reg[arg];

    inst = inst.match(/[0-9a-z]+/g);
    console.log(inst);
    switch(inst[0]) {
    	case 'push':	stack.push(getArg(inst[1])); break;
    	case 'pop':	if (inst[1]) reg[inst[1]] = stack.pop(); 	else stack.pop(); break;
    	case 'pushr': stack.push(reg['a'],reg['b'],reg['c'],reg['d']); break;
    	case 'pushrr': stack.push(reg['d'],reg['c'],reg['b'],reg['a']); break;
    	case 'popr': [reg['d'],reg['c'],reg['b'],reg['a']] = stack.splice(-4,4); break;
    	case 'poprr': [reg['a'],reg['b'],reg['c'],reg['d']] = stack.splice(-4,4); break;
    	case 'add':	reg['a'] = Array.from({length:getArg(inst[1])},()=>stack.pop()).reduce((a,c)=>a+c); break;
    }
    

    }
    return { exec }
    }

  • Default User Avatar

    Very good Kata!
    ...
    Nice job, mr.MindWanderer!

  • Default User Avatar

    I think this Kata has some mistakes(

    • = Thou shalt not clobber thy input! = - Expected: [[8.88, 7.96], [7.48, 1.91]], instead got: [[7.48, 1.91], [8.88, 7.96]]
      8.88 * 7.96 is less then 7.48 * 1.91 ?