Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let step = (base, exponent) => {
	exponent = exponent || 2;
	
	/* Ternary operator(other way):
	 * (exponent == undefined) ? exponent = 2 : exponent;
	 */
    
  	let res = 1;
  	
	for (let c = 0; c < exponent; c++)
    	res *= base;
  	
	return res;
}

// Examples:

step(2) // -> 4
step(2,2) // -> 4
step(1.5) // -> 2.25
step(-2,4) // -> 16
step(-1.5,3) // -> -3.375
step(1.1, 2) // -> 1.2100000000000002; must fix it with toFixed(3)
step(1.1, 3) // -> 1.3310000000000004; must fix it with toFixed(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment