Skip to content

Instantly share code, notes, and snippets.

@RinatValiullov
Last active November 9, 2017 12:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RinatValiullov/0e672acd5c212e983039e4f269554fbc to your computer and use it in GitHub Desktop.
Save RinatValiullov/0e672acd5c212e983039e4f269554fbc to your computer and use it in GitHub Desktop.
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