why doesnt the persian word add behind a number? - javascript

i have a really strange problem in java script.
look at these codes and run the app:
const number = 200000;
const persianMoney = "تومان";
//pay attention to these lines:
console.log("way 1:");
console.log(number + persianMoney);
console.log(persianMoney + number);
console.log("way 2:");
console.log(`${number}${persianMoney}`);
console.log(`${persianMoney}${number}`);
console.log("way 3:");
console.log(String(number) + persianMoney);
console.log(persianMoney + String(number));
console.log("way 4:");
console.log(`${String(number)}${persianMoney}`);
console.log(`${persianMoney}${String(number)}`);
console.log("way 5:");
console.log(String(number + persianMoney));
console.log(String(persianMoney + number));
all of the outputs are the same!! all of the outputs are 200000تومان! but why word تومان is not behind 200000 in some outputs? even here, i cant put تومان behind 200000! but why it is like that? i cant understand it. i tested concatenation in 5 ways but none of them were correct! how can i solve this problem? thanks for helping.

As stated by Raymand Chen. My understanding is that you want to have the currency name تومان after the number 200000. To keep the Arabic/Persian word in the same direction as the Latin word i.e. in a Left-To-Right LTR direction.
One possible way to do that is to add the LTR code \u200E
const number = 200000;
const persianMoney = "تومان";
const ltr = "\u200E";
console.log(persianMoney + ltr + " " + number);

Related

Is the \n supposed to indent your text?

I'm a python programmer and in Python, the \n renders a new line like so:
print("Hello \n World")
Hello
World
But I'm trying to do that in Javascript with the following block of code:
if (userInput == wrongAnswer){
let finalScore = initialScore + scoreForA;
console.log(finalScore);
if (finalScore == 5){
console.log(rightScore);
}
else{
console.log("Initial score:", initialScore, "\n", outputA, "\n", "Final score:", finalScore);
}
}
Edit: By the way, I've defined these variables already.
But it gives me:
And I'm supposed to make it:
Is the \n supposed to auto-indent Wrong answer and Final score after making a new line in JavaScript?
Note that you're providing multiple arguments to console.log, so the python analog would be print("Hello\n", "World") which would add a space on the second line as well. Multiple arguments are always separated by a space, if you don't like that, concatenate them or use a template literal:
console.log(`Initial score: ${initialScore}\n${outputA}\nFinal score:${finalScore}`)
I think it is because the function console.log() adds a space between each params.
You can do that :
console.log("Initial score" + 5 +"\n" + "WrongAnswer" +" :(" + "\n" + "Final score -1");

How to do PigLatin in Javascript

Can somebody please help me with PigLatin? I'm trying to solve is problem here and my code works most of the time but I can't make some parts of it work.
For example when I give a string to the function like quiet or square, I want the function to take the qu part as 1 letter. So quiet would be ietquay and square would be aresquay..
Would it also be possible to make it work when I give it a long string with multiple words? Do the function on every word in the string basically..
My code so far:
const piglatin = string => {
let firstVowel = string.match(/[aeiou]/);
let firstPosition = string.indexOf(firstVowel);
if (firstPosition > 0) {
return string.slice(firstPosition) + string.slice(0, firstPosition) + 'ay';
}
return string + "ay";
}

Formatting integers with space as thousands separator

I wanted 1000 to look like 10 000. There are tons of examples to make a separator but they all show you how to start using comma or some StringLocal. How do I use space instead? Which locale should I use?
I have already explained how mine question is different. I have asked it just because that solution does not suit me. I am not happy with commas. It is rediculous to hear that crucial difference = the question is duplicate.
Here is my solution:
var number = 15000; //Put your number
var numstring = number.toString();
if(numstring.length > 3){
var thpos = -3;
var strgnum = numstring.slice(0, numstring.length+thpos);
var strgspace = (" " + numstring.slice(thpos));
numstring = strgnum + strgspace;
}
console.log(numstring);

Formatting bible verse reference with regex in javascript

I'm trying to monospace bibleverse references so that single digit chapters or verses have a leading space.
So "4:5" becomes " 4: 5" and "3:21" becomes " 3:21".
I'm really having problems writing the regex, please help.
I've tried many variations but they essentially boil down to (^\d|\d$), (^\d{1}|\d{1}$) and (^[^0-9]\d|[^0-9]\d$) and many combinations between them
inRef = inChapter + ':' + inVerse;
var inReg = /(^[0-9]{1}|[^0-9][0-9]{1}$)/g;
inRef = inRef.replace(inReg," $1");
console.log(inRef);
Alot of the results I'm getting from my efforts turn references like "6:15" into " 6: 1 5" or " 6:1 5"
Thank you in advance.
Why a regex at all? You've already got the chapter/verse as separate data BEFORE you combined them into the x:y format, so do the formatting there while they're still seperate strings:
if (inChapter.length == 1) { inChapter = ' ' + inChapter }
inRef = inChapter + ':' + inVerse;
Using a regex for this uber-simplistic transformation is akin to nuking a city to get some dust off a shelf instead of using a feather duster.
Given the strings inChapter and inVerse, you could do something like this:
inRef = (" " + inChapter).slice(-2) + ":" + (" " + inVerse).slice(-2)
Note there are two spaces there " " and I'm assuming inChapter and inVerse are only ever 1 or 2 digits.
Edit: Since you need three digits and I assume you still want these to line up, you could do this:
var pad = " "; // this is now THREE spaces!
inRef = (pad + inChapter).slice(-pad.length) + ":" + (pad + inVerse).slice(-pad.length)
So now if you run all your inChapter and inVerse pairs through this, you should get strings that line up like this:
100:100
20:100
2:100
100: 10
100: 1
10: 10
10: 1
1: 1

Javascript: quick script to line up pipe-delimited text (or any-character-delimited text)

Background: I've written this before but I don't like the approach. The reason is because Javascript does not have "sprintf" and that is something I use heavily if the language supports it.
Question: How would you use javascript to go from BEFORE to AFTER? If anyone has a solution with very small number of lines of code, or something from a javascript string library, that would be informative. TIA.
BEFORE:
red| lightblue| green
cherry| ice| mint
round| cubic| flowery
AFTER:
red | lightblue | green
cherry | ice | mint
round | cubic | flowery
Disclaimer: This is not homework or any such thing, just looking for new ideas. Also, this is not browser-based javascript. This is not a web-development question, but a javascript programming question.
If you like sprintf, why not look for a JavaScript implementation for it?
function pad(str, len) {
for (var count = len - str.length; count > 0; count--) {
str = str + " ";
}
return str;
}
console.log(pad("red", 7) + "| " + pad("lightblue", 9) + "| " + pad("green", 7));
//etc.
Yeah, concatenating characters one by one is inefficient, but generally you'll only have a small number of iterations.

Categories