Separate an amount in thousands, hundreds, tens and units

0

I have the following code that should be put in control of the image interface but it does not work. Please some advice so that the amount that I send to a text box and show me how many thousands, hundreds, tens and units has.

How can I separate an amount in thousands, hundreds, tens and units and count how many of each unit?

function separarSifras(cantidad) {
  if (cantidad == "") {
    alert("Escribe una cantidad por favor");
  } else {
    if (!isNaN(cantidad.value)) {
      alert("Escibe solo numeros");
    } else {
      var cadeSifra = new String(cantidad.value);

      var longitudSifra = cantidad.length;

      var millares = "";
      var centenas = "";
      var decenas = "";
      var unidades = "";

      for (cont = 0; cont < longitud.length; cont++) {
        if (cadeSifra.charAt(cont) == '1') priNum++;


      }
    }
  }
}
<h1 align="center">Clasificador de monedas</h1>
<form>
  <table align="center" border=0>
    <tr>
      <th>
        Ingresa una cantidad:
        <input type="text" name="cantidad" maxlength="5" />
        <input type="button" value="Acomodar" onClick="separarSifras(this.form.cantidad)" />
      </th>
      <td align=center>
        <input type="button" value="Reiniciar" onclick="window.location.reload()" />
      </td>
    </tr>
  </table>
</form>
<br>
<table align="center" border="1">
  <tr>
    <td>Millares</td>
    <td>Centenas</td>
    <td>Decenas</td>
    <td>Unidades</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

I get the error

  

ReferenceError: length is not defined

    
asked by Jesús 05.02.2018 в 04:56
source

2 answers

2

A simple way to decompose the number is to create an array of characters from the value and extract them one by one.

function separarSifras(cantidad) {
  
  if (cantidad.value == "") {
    alert("Escribe una cantidad por favor");
    return;
  }

  // Obtener valor numérico
  var valor = parseInt(cantidad.value);

  if (isNaN(valor)) {
    alert("Escibe solo numeros");
  } else {
    // Crear array de caracteres a partir del número
    var cadeSifra = valor.toString().split("");

    document.getElementById('unidades').innerText = 
      cadeSifra.length ? cadeSifra.pop() : '';
    document.getElementById('decenas').innerText = 
      cadeSifra.length ? cadeSifra.pop() : '';
    document.getElementById('centenas').innerText = 
      cadeSifra.length ? cadeSifra.pop() : '';
    // Convertimos el resto del array a cadena
    document.getElementById('millares').innerText = 
      cadeSifra.length ? cadeSifra.join('') : '';
  }
}
<h1 align="center">Clasificador de monedas</h1>
<form>
  <table align="center" border=0>
    <tr>
      <th>
        Ingresa una cantidad:
        <input type="text" name="cantidad" maxlength="5" />
        <input type="button" value="Acomodar" onClick="separarSifras(this.form.cantidad)" />
      </th>
      <td align=center>
        <input type="button" value="Reiniciar" onclick="window.location.reload()" />
      </td>
    </tr>
  </table>
</form>
<br>
<table align="center" border="1">
  <tr>
    <td>Millares</td>
    <td>Centenas</td>
    <td>Decenas</td>
    <td>Unidades</td>
  </tr>
  <tr>
    <td><span id="millares"></span></td>
    <td><span id="centenas"></span></td>
    <td><span id="decenas"></span></td>
    <td><span id="unidades"></span></td>
  </tr>
</table>
    
answered by 05.02.2018 в 09:43
0
  

I get the error

     
    

ReferenceError: length is not defined

  

The indicated error is shown when the input value is not a number when it should be the opposite. Change

if (!isNaN(cantidad.value)) {

for

if (isNaN(cantidad.value)) {

In addition, the code has other errors not related to the title of the question, some already mentioned in comments as the use of undefined variables, such as longitud and primNum and others as the publication of the result.

    
answered by 05.02.2018 в 16:45