Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39448

Understanding recursive code

Newsgroups comp.lang.javascript
Date 2021-02-10 01:36 -0800
Message-ID <fb7855df-e736-40db-b184-8621c13f4d1bn@googlegroups.com> (permalink)
Subject Understanding recursive code
From Jonas Thörnvall <jonas.thornvall@gmail.com>

Show all headers | View raw


I had a look at my own recursive code, and realised i am not sure i get what actually break my recursion. Could someone explain.

calcSTR="9*9"

function evalPrimary(calcSTR){
	console.log("evalPrimary");
	//var newSub=calcSTR;
	for(m=0;m<calcSTR.length;m++){
	tempChar=calcSTR.charCodeAt(m);
	    // if multiply or divide
		if(tempChar==42 || tempChar==47 ){
			operand=calcSTR.charAt(m);
			getPrimaryOps(calcSTR);
			calcSubPrimary(calcSTR);
			m++;
		}
	}
}

function getPrimaryOps(calcSTR){
	console.log("getPrimaryOps");
	//First operand
	p=m;op1="";
	while((calcSTR.charCodeAt(p-1)>47 && calcSTR.charCodeAt(p-1)<58) || calcSTR.charCodeAt(p-1)==46){
		op1=calcSTR.charAt(p-1)+op1;
		p--;
	}
	if (calcSTR.charAt(p-1)=="-") {sign="-";} else {sign="+";}
	cutStart=p;
	n=m;op2="";
	//Second operand
	if (calcSTR.charAt(n+1)!="-"){sign2="+";}
	else if (calcSTR.charAt(n+1)=="-"){sign2="-";n++;}
	while((calcSTR.charCodeAt(n+1)>47 && calcSTR.charCodeAt(n+1)<58) || calcSTR.charCodeAt(n+1)==46){
		op2=op2+calcSTR.charAt(n+1);
		n++;
	}
	cutEnd=n;
}

function calcSubPrimary(calcSTR){
	console.log("calcSubPrimary");
	if (evaluation){outSTR+="\nSubStr PRE IN to resolve "+calcSTR;}
	subResult=eval(sign+op1+operand+sign2+op2);
	console.log(outSTR);
	resSign=Math.sign(subResult);
	if (resSign==1){subResult="+"+subResult;}
	subStart=calcSTR.substring(0,cutStart-1);
	subEnd=calcSTR.substring(cutEnd+1,calcSTR.length);
	if (evaluation){outSTR+="\nSub StartStr "+subStart+"\n";}
	if (evaluation){outSTR+="Sub result "+subResult+"="+sign+op1+operand+sign2+op2+"\n";}
	if (evaluation){outSTR+="Sub EndStr "+subEnd+"\n";}
	newSub=subStart+subResult+subEnd;
	if (evaluation){outSTR+="SubStr PRE OUT "+newSub+"\n";}
	evalPrimary(newSub);
}

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Understanding recursive code Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-10 01:36 -0800
  Re: Understanding recursive code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-02-10 11:19 +0000
    Re: Understanding recursive code Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-10 05:19 -0800
      Re: Understanding recursive code John G Harris <niam@jghnorth.org.uk.invalid> - 2021-02-10 18:01 +0000

csiph-web