Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Un bucle parece no romperse nunca/el sistema está sobrecargado

En la escuela, tenemos que escribir una función que cifre un determinado mensaje moviendo cada letra del mensaje por un número 'nC' (o niveauCryptage) en el alfabeto.

Actualmente, cada vez que ejecuto el código, me quedo atascado en una pantalla de carga permanente después de completar las dos interacciones del usuario (las que preguntan el número por el cual se cambiarán las letras en el alfabeto y el mensaje que el usuario quiere encriptar). Sospecho que algo anda mal con los bucles.

 function crypter(t, l, nC) { //This function receives the alphavet Table, a letter, and the number by which the letter has to be shifted in the alphabet. //This function returns a single crypted letter... var crypte = ''; var i = 0; while (t[i] != l) { i++; } if (i + nC > t.length) { crypte = (i + nC) - (t.length); } else { crypte = i + nC; } return crypte; } //Global variables var tAlphaB = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' ]; var niveauCryptage = 0; var texteBase = ''; var tTexteAvant = new Array(); var tTexteApres = []; var txt = 'Le résultat du cryptage est : \n\n'; //Main program //Ask the user to specify the number by which the letters of the message have to be shifted in the alphabet //This number has to be a number between 1 and 10 (1 and 10 included) do { niveauCryptage = parseFloat(prompt("Saisissez un nombre entre 1 et 10 pour selectionner le niveau de cryptage")); } while (isNaN(niveauCryptage) || niveauCryptage > 10 || niveauCryptage < 1) //Ask the user to enter a message on the condition that it is a chain of characters do { texteBase = prompt("Quel texte voudrez-vous crypter?"); } while (!isNaN(texteBase)) //Transform the message into the table tTexteAvant using the split('') function tTexteAvant = new Array(texteBase.split('')); //Encrypt each letter of the table, tTexteAvant, into a new table, tTexteApres var j = 0; while (j < tTexteAvant.length) { tTexteApres.push(crypter(tAlphaB, tTexteAvant[j], niveauCryptage)); j++; }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El tTexteAvant = new Array(texteBase.split('')); creará una matriz con un solo elemento que será la matriz con los caracteres.

Lo más probable es que desee tTexteAvant = texteBase.split('');

about 4 years ago · Juan Pablo Isaza Report

0

Como se indica en el comentario de Jesse, esta parte de la función de crypter entra en bucle infinito cuando el carácter l no está en la matriz, es decir, cuando es un carácter en minúsculas o de puntuación, por ejemplo.

 while (t[i] != l) { i++; }

Si solo necesita encontrar si un elemento de una cadena está presente en una matriz, puede reemplazar esa parte con una función de matriz que encuentre el valor y devuelva el índice en la matriz (o -1 si no se encuentra):

 i = t.indexOf(l);

Luego, debe decidir qué hacer con los caracteres no válidos o no reconocidos o solicitar al usuario que ingrese una cadena válida (solo caracteres alfabéticos en mayúsculas y espacios). También puede decidir transformar los caracteres en minúsculas en mayúsculas, si esto es aceptable.

Como nota al margen, verifique el valor de retorno de la función de crypter : devuelve el nuevo índice y no la nueva letra. Como se comentó al principio de la función, este no es el comportamiento esperado.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!