I am currently learning a bit. In one script I get a number which lenghts usually is 4. In rare cases it can be a 8 digit number. If it's 8 digits long I need to save it in a different variable.
So I get my variable, lets call it mynumber
.
Now I printed mynumber.length
which is mostly 4. If it is greater than 4 I want to store my original variable in mynumber2
and delete it from mynumber
.
What's a good way to achive that?
I am currently learning a bit. In one script I get a number which lenghts usually is 4. In rare cases it can be a 8 digit number. If it's 8 digits long I need to save it in a different variable.
So I get my variable, lets call it mynumber
.
Now I printed mynumber.length
which is mostly 4. If it is greater than 4 I want to store my original variable in mynumber2
and delete it from mynumber
.
What's a good way to achive that?
Share Improve this question asked Feb 14, 2018 at 10:53 RayofCommandRayofCommand 4,27417 gold badges58 silver badges95 bronze badges 4- 2 Show the code please so we can help you. – Manuel Perez Heredia Commented Feb 14, 2018 at 10:55
- 2 You already have the pseudocode, just convert it. – void Commented Feb 14, 2018 at 10:56
- 1 What does the tag servicenow have to do with the question? For somebody with > 1k rep I would expect more from both the question format as the tags used – Icepickle Commented Feb 14, 2018 at 10:58
- I will update in a minute, I use servicenow server side scripts. which is basically javascript + glide – RayofCommand Commented Feb 14, 2018 at 11:02
2 Answers
Reset to default 4Try this;
myNumber = 1234; // Or whatever is the value
if(myNumber.length > 4) // If length greater than 4
myNumber2 = myNumber;
myNumber = undefined; // If you want to delete it from original number variable
delete myNumber; // If you want to delete the original number variable
var x = 12345;
var mynumber = 0;
var mynumber2 = 0;
(x.toString().length > 4 ? mynumber2 = x : mynumber = x);
console.log(mynumber + ' - '+ mynumber2);