JavaScript - Proyecto Maximo Comun Divisor, javascript

 
Vista:
sin imagen de perfil

Proyecto Maximo Comun Divisor, javascript

Publicado por Daniel (2 intervenciones) el 12/03/2021 12:33:18
Hola, me han mandado un ejercicio de hacer el máximo común divisor, y he encontrado este código, pero yo estoy empanzando y es muy complejo, ¿podría alguien simplificarle?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<html>
<head>
</head>
<body>
    <form id="MCD" name="MCD" style="padding-top: 1rem">
    <label forHTML="dataInput">Introduzca números enteros separados por coma: </label>
    <br><br>
    <input type="text" id="dataInput" name="dataInput" size="30" placeholder="32,12,96" pattern="^\d+[0-9,]+\d+$" required/>
    <button type="button" onclick="calcularMCD()">Calcular</button>
    <br><br>
    <div id="MCD"></div>
  </form>
  <script>
 
function MCD(){
  if (arguments.length<2) return false;
  if (arguments.length==2)return (arguments[1]==0?arguments[0]:MCD(arguments[1],arguments[0]%arguments[1]));
  var arr=[].splice.call(arguments,0);
  arr.splice(0,2,MCD(arr[0],arr[1]));
  return MCD.apply(window,arr);
}
 
function calcularMCD() {
  var input = document.getElementById('dataInput').value;
  var numbers = [];
  var idx = 0;
 
 
  for(var i = 0; i < input.length; i++) {
    if (input[i] == ',') {
      numbers.push(parseInt(input.substring(idx,i)));
      idx = i + 1;
    } else if (i == input.length - 1) {
      numbers.push(parseInt(input.substring(idx)));
    }
  }
 
  document.getElementById('MCD').innerText = 'El MCD es: ' +  MCD.apply(window, numbers);
 
  console.log('MCD:', MCD.apply(window, numbers));
 
}
</script>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder