JavaScript Text Direction

Oct 07, 2022

I have a challenge with text direction. I usually use Persian/Arabic language which is Right to Left direction.I also used JavaScript code in it and wrote something in Persian/Arabic language in parts of it. But in the output, due to the use of Persian language, it shows the numbers in reverse. For example, the number 1234 is represented by 4321, or the word "null" is represented by "llun". Please tell me a solution to my problem.

Output

8 Replies
Mahdi Malekan

Yes, you are correct.but Reversing the numbers doesn't solve the problem because the same thing happens with words

Actually, I translated some English words in the codes into Farsi.

I will send the code link below for you to take a look.

https://stackoverflow.com/questions/73748948/text-direction-issue-in-javascript

 

Mahdi Malekan

sorry for late reply. I did not have access to the Internet due to issues in my country

Ok, I will send you the project...
This project works like this:
You enter the user's name and the user's score and then press the blue button. The box below shows the user's rank based on his score.
The javascript code is in the blue button and the data is stored in a google sheet.

Math Notermans

Basically i added a function to reverse any part of your text to it.

reverse
As you can see the first line both the number and my name are RTL... As i am not 100% sure if that was correct...i added a function to get parts of your string and revert those.

function reverse(s){
    return s.split("").reverse().join("");
}

This is the function that does that. In the Storyline added i commented a lot, just to see if it works. And i pass the name and score to a separate array... that might not be necessarry... you have to test that. Only thing thats really needed is converting any value sending to reverse function into a string ( toString() ) Either directly, or in the function itself.

Just tried that by changing the function to this:

function reverse(s){
    s = s.toString();
    return s.split("").reverse().join("");
}

Works fine.

Mahdi Malekan

Hi Again Math

Wow!! , it seems that you have found the solution! It was really amazing.

But since I am not familiar with the JavaScript language, I could not understand the codes.

The user score is now working correctly but with many attempts I could not reverse the date and time like you.


I hope you can help me in this matter too, dear Math.