JSFiddle - React, Tailwind, and code Playground

by jmcjc5u

JavaScript

// example 1: factorial (5) = 120

function fac (n) {
	if (n == 1) {
  	return 1
  } else {
  	return n*fac(n-1)
  }
}

//alert ("factorial (5) = " + fac(5))

// example 2: a x^2 + bx + c = 0

function quadratic (a, b, c) {
	var det = b*b - 4*a*c;
  
  // []: array
  if (det == 0) 
  	return [-b/2/a]  // one root, array with ONE element
  else if (det > 0) {
  	var tmp = Math.sqrt(det)
  	return [(-b+tmp) /2/a, (-b-tmp)/2/a ]  // array with TWO distinctive element
  } else // det < 0
  	return []  // array with NO element
  
}

var result1 = quadratic (1, -2, 3)
console.log  ('the root is: ' + result1[0])

//var result2 = quadratic (1, -2, 3)
//var result3 = quadratic (1, -1, -2)