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

159
Views
How to swap first and last digits of a number using for loop in Javascript

I need to ask user to input a 3 digit number and then swap the first and last numbers using a for loop. This is what I have so far, but I'm stuck. Using a for loop seems illogical, but that is what I have to do:

num = prompt("Please input a number with 3 digits.");
let firstDigit = num[0];
let secondDigit = num[1];
let lastDigit = num[2];

for (firstDigit < 10; secondDigit < 10; lastDigit < 10); {
    console.log(lastDigit + secondDigit + firstDigit);
}

Please help!

Thanks

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

0

is it help for you?

// let num = prompt("Please input a number with 3 digits.");

// if (num.length > 3) alert("3 digits please");
// else {
//   let answer = "";
//   for (var i = 2; i >= 0; i--) {
//     answer = answer + num[i];
//   }
//   console.log(answer);
//}

let num = prompt("Please input a number");
let temp = "";
let answer = "";

for(let i = 0 ; i < num.length; i++) {
    if (i === 0) temp = num[i]; // save first number to temp
    else if (i === num.length - 1) {
        // When the last number is encountered, the last number is put at the beginning, and the first number stored in temp is put at the end.
        answer = answer + temp;
        answer = num[i] + answer;
    }
    else answer = answer + num[i];
}
console.log(answer);

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!