pballett/whatfreewords

Possible issue in function `F`

jmetz opened this issue · 1 comments

jmetz commented

Firstly great work! I was trying to understand your code, and if I've understood correctly your function F might not be working as you intended, though perhaps it doesn't matter 🤷

Your code is :

function F(L,K){
	x = parseInt(L);
	k = parseInt(K);
	temp = ""+x*x*x*K*3+23*K*K*x*x*x+K*(x+K)+3;
	return temp.slice(4,10);
};

(https://github.com/pballett/whatfreewords/blob/master/index.html#L40-L45)

and specifically where you assign temp, you are doing an odd mix of numerical and string ops - basically the + operations are all doing string concatenation, as you operate on K and not k (perhaps you meant to use k? At the moment it's not used at all).

I would recommend changing to the following which I tested locally and also works:

function F(L,K){
	x = parseInt(L);
	k = parseInt(K);
	temp = x*x*x*k*3+23*k*k*x*x*x+k*(x+k)+3;
	return (""+temp).slice(4,10);
};

Here as you can see I operate on k (lowercase), and the assignment to temp returns a number, not a string. That is then coerced to a string on the following line before being sliced and returned.

Hey, great catch. Yeah that's a typo, which I suppose I never noticed because F is essentially arbitrary anyway. Thanks again.