'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



Solution 1:[1]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1