最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

servicenow - JavaScript Check length of String and if it's longer than 4, save variable in different variable - Stack Ov

programmeradmin8浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

Try 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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论