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.
Related
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"
This question already has answers here:
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Converting php string to Title Case
(3 answers)
Closed 4 months ago.
I would need a Custom JavaScript for a WordPress form, converting upper cases to lower cases. I found some entries here in the community, but not the one I need.
What should be converted ?
For example I get a form entry 'PETER' and I want that the first digit 'P' stays in upper cases and the rest should be converted to lower cases. From 'PETER' to 'Peter'
Who can give a helping hand, please ? Thanks
You can get the sting first char with str[0], the other parts sliced with -1 argument.
const name = "PETER";
function PETERToPeter(str) {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
const nameChanged = PETERToPeter(name);
console.log(nameChanged);
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
What's the best way to convert a number to a string in JavaScript?
(25 answers)
Closed 2 years ago.
I know that this question was asked about in PHP but I could not find anything in javascript.
I have a random number generated between 000(yeah it is just three zero's but it is shown that way from how the number is generated) and 999999999 and I want to test true or false for if whether it includes a specific sequence of numbers like 777, 9000, 28, or the like of any length and from any beginning of numbers...
for example, finding 289 in 678342891 or 728987699 would be true, and finding 289 in 678529187 or 023829564 would be false.
is this possible, and how would I do it?
you can use .includes method in JS after transforming both the number and the other number to strings using .toSting method
let n = 12345589;
let sub = 55;
let sub2 = 25;
function isSeq(number, sub){
number = number.toString(10);
sub = sub.toString(10);
return number.includes(sub);
}
console.log(isSeq(n, sub));
console.log(isSeq(n, sub2));
Check whether your test number is a substring in your long random number. Since you have mentioned that 000 is a value, this tells me that it is a string already(no need to call toString() for it)
var random=678342891;
var search1=289;
var search2=777;
console.log( random.toString().indexOf(search1.toString()) != -1 );//true
console.log( random.toString().indexOf(search2.toString()) != -1 );//false
A function to test it would look like so:
function test(random,search){
return random.toString().indexOf(search.toString()) != -1
}
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.
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);