Weird add many Char in javascript [duplicate] - javascript

This question already has answers here:
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
(8 answers)
Closed 3 years ago.
Today i saw an example of JavaScript and i can not understand it.
('b' + 'a' + + 'a' + 'a').toLowerCase()
result: "banana"
I don't understand it, why JavaScript add 2 'n' between each 'a'?

The first ‘b’ and ‘a’ are simply strings being added as ‘ba’. After the second ‘a’, you see a double plus sign(+), the first one is for concatenation like the previous plus sign. But the second plus sign is called the unary operator which simply transforms the string into a number, if it isn’t already. Since ‘a’ cannot be converted to a number it is converted to ‘NaN’. The final ‘a’ is added to this ‘baNaN’ string and the final ‘baNaNa’ string is made. And to finish it up, the toLowerCase function is used and the output ‘banana’ is received.

Related

How come the following code doesn't change the retrieved letters into upper case? [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Replace method doesn't work
(4 answers)
Closed 23 days ago.
The goal of this was to change the retrieved letters into upper case. I know this works if I stored lines 6 and 7 inside a variable then replace the letters inside the variable called string, but I wanted to know why this code doesn't work instead.
JavaScript strings are not mutable, meaning you can't change the value of a character at a certain index. You'll need to make a new string made of these values.
let string = 'lowercasestring'; // Creates a variable and stores a string.
let letterOne = string.indexOf('l'); // Should store as 0.
let letterTwo = string.indexOf('s'); // Should store as 7.
// mutiple lines for readability
string = string[letterOne].toUpperCase() +
string.slice(letterOne + 1, letterTwo) +
string[letterTwo].toUpperCase() +
string.slice(letterTwo + 1);
console.log(string);
Output:
"LowercaSestring"

How to specify types in javascript [duplicate]

This question already has answers here:
JavaScript adding a string to a number
(8 answers)
Closed 3 years ago.
Hi! My problem is that when you prompt a number and add it to another prompt is uses the "+" operator to join the lines. I want it to add the numbers together. How can I do this?
alert(prompt('add a number') + prompt('add another number'));
Thanks in advance,
TheCoder
You can convert a string to a number with the + operator, like so:
+"50" + +"40" = 90
The result will be NaN if the strings can't be converted to a number (ie, +"fifty" => NaN), so you'll want to account for that in your code.
alert(+prompt('add a number') + +prompt('add another number'));
You need to use parseInt or parseFloat
alert(parseInt(prompt('add a number')) + prompt(parseInt('add another number')));

Javascript slice concatenation [duplicate]

This question already has answers here:
JavaScript slice method?
(6 answers)
Closed 4 years ago.
I need to bump a string down 1 index thus moving index 0 to the back.
For example, turn the string '12345' into '23451'. The code below works but I just don't understand why/how.
How does the return statement remember to add '345' back into string s? Shouldn't it be returning the concatenation '21'?
let s = "12345"
let rotate = (function (){
return s.slice(1) + s.slice(0,1);
})
console.log(rotate(s))
"Shouldn't it be returning the concatenation '21'?"
s.slice(1) does not return an element at index 1, but everything that starts from index 1. So in your case it will stand for 2345 and finally will result in 23451.
The slice takes two arguments. If there is only one argument present, it will return the string FROM the first index (missing everything before that). If there are two arguments present, it will return a string containing the characters between the two indices. For this example, it is the first character (from 0 to 1). Then it just adds these two parts together and returns it.
You can read more about the slice function on Mozilla Docs.

JS how to add instead of concatenate numbers [duplicate]

This question already has answers here:
Javascript variables not adding two variables correctly, only concatenating
(3 answers)
Closed 4 years ago.
This question has an answer:
see - Javascript variables not adding two variables correctly, only concatenating
I am trying to add 5 units to a number but the number is being concatenated instead.
this.graphicState[i].shapes[j][k].x += 5
Each time this is run in a loop the outputs are
105.00
105.005
105.0055
105.00555
...
The output I am looking for is,
105.00
110.00
115.00
120.00
...
I tried,
this.graphicState[i].shapes[j][k].x += parseFloat(5)
I also tried this, but get the same results,
this.graphicState[i].shapes[j][k].x = this.graphicState[i].shapes[j][k].x + 5
Thanks,
You need to convert any/all string values to numbers in an expression that has strings as operands with + as the operator. The 5 isn't the issue, this is:
this.graphicState[i].shapes[j][k].x
So, that's what needs to be converted. You can do that easily by prepending a + to it:
+this.graphicState[i].shapes[j][k].x;
Do the conversion on the string first and then use the converted value in your mathematical expression. Here's a simplified example:
var result = "5"
result = +result + 10;
console.log(result);
Try this method
this.graphicState[i].shapes[j][k].x = (parseFloat(this.graphicState[i].shapes[j][k].x) + 5).toFixed(2);

Different Javascript behavior on substraction and sum [duplicate]

This question already has answers here:
Javascript addition and subtraction with a string value
(8 answers)
Closed 8 years ago.
Why such thing would happen in Javascript?
'5'+3 = 53
'5'-3 = 2
This is happening because + operator is overloaded. If any operand is a string, string concatenation is performed. If you have two numbers, addition is performed.
In other words
2+3=5
while '2'+3='23' and 2+'3'='23'.
On the other hand, for the - operator, it is not overloaded in such a way and all operands are converted to numbers.
'8'-2=6
because - is not overloaded and operand '8' will be converted to 8. Hence, we get 6.
For further information on this, please have a look here and read the paragraphs 11.6.1 and 11.6.2.
String concatenation is done with + so Javascript will convert the first numeric 5 to a string and concatenate "5" and "3" making "53".
You cannot perform subtraction on strings, so Javascript converts the second numeric i.e. "3" to a number and subtracts 3 from 5, resulting in "2" as the result.

Categories

Resources