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

320
Views
Reverse () no funciona y también lo hace unshift () en javascript de vainilla

El problema solo está ahí cuando trato de revertir la entrada del usuario, reverse() está devolviendo la cadena tal como está y también lo hace unshift() .

lo que estoy buscando es si el usuario ingresa input - 1234 la output should be 4,3,2,1 , pero estoy obteniendo output - 1,2,3,4 .

 const pahere = []; const revephare = []; let donereve = []; let dff = document.getElementById("Udt1"); function tex(){ if(dff. value == "") { console.log("enter text") } else { pahere.push(dff.value); console.log(dff.value); console.log(pahere); console.log(pahere.length); for(let i = 0; i < pahere.length; i++){ revephare.push(pahere[i].split("")); pahere.pop(); } } console.log("I should be splited",revephare); donereve = revephare.reverse(); console.log("I should be reversed",donereve); }
 * { box-sizing: border-box; } body{ background: #333; margin: 0; } h1{ margin: 15px; color: white; } .holder{ margin-left: 12px; width: 34em; height: 37em; border: 2px solid rgba(255, 51, 153,1); display: flex; } #Udt1 { width: 56%; height: 2em!important; text-align: center; } span { color: white; margin-top: 5px; } .anshold { width: 154px; height: 34px; margin-left: 43em; position: relative; top: -592px !important; border: 2px solid rgba(255, 51, 153,1); text-align: center; } #udans{ color: white; text-align: center; margin: 0; padding: 4px; } .btn { width: 16%; height: 2em!important; text-align: center; margin-left: 14px; }
 <body> <h1>Palidrom Checker</h1> <div class="holder"> <span>Word goes here-</span> <input type="text" name="textin" label ="textin" id="Udt1" placeholder="Eg-Racecar"> <button class="btn" onclick="tex()"> Check</button> </div> <div class="anshold"><p id="udans"> </p> </div> </body>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Hola, sé que solucionaste el problema, puedes lograr tu salida con este código de una sola línea, solo inténtalo si puedes

 let reversed=(pahere.toString()).split("").map(Number).reverse();
about 4 years ago · Juan Pablo Isaza Report

0

Estás tratando de dividir Array pahere[i].split("")

 const pahere = []; const revephare = []; let donereve = []; let dff = document.getElementById("Udt1"); function tex(){ if(dff. value == "") { console.log("enter text") } else { pahere.push(dff.value); console.log(dff.value); console.log(pahere); console.log(pahere.length); for(let i = 0; i <dff.value.length; i++){ revephare.push(dff.value[i]); pahere.pop(); } } console.log("I should be splited",revephare); donereve = revephare.reverse(); console.log("I should be reversed",donereve); }
 * { box-sizing: border-box; } body{ background: #333; margin: 0; } h1{ margin: 15px; color: white; } .holder{ margin-left: 12px; width: 34em; height: 37em; border: 2px solid rgba(255, 51, 153,1); display: flex; } #Udt1 { width: 56%; height: 2em!important; text-align: center; } span { color: white; margin-top: 5px; } .anshold { width: 154px; height: 34px; margin-left: 43em; position: relative; top: -592px !important; border: 2px solid rgba(255, 51, 153,1); text-align: center; } #udans{ color: white; text-align: center; margin: 0; padding: 4px; } .btn { width: 16%; height: 2em!important; text-align: center; margin-left: 14px; }
 <body> <h1>Palidrom Checker</h1> <div class="holder"> <span>Word goes here-</span> <input type="text" name="textin" label ="textin" id="Udt1" placeholder="Eg-Racecar"> <button class="btn" onclick="tex()"> Check</button> </div> <div class="anshold"><p id="udans"> </p> </div> </body>

about 4 years ago · Juan Pablo Isaza Report

0

Esto se debe a que está utilizando Array.prototype.push incorrectamente, lo que le da como resultado una matriz de matrices.

 revephare.push(pahere[i].split("")); // this line is incorrect

Reemplázalo por lo siguiente para que funcione

 // use spread operator to pass each element as a separate argument revephare.push(...pahere[i].split(""));
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!