Solved

how do I cut up a number like 281 in javascript

Posted on 2012-03-18
9
245 Views
Last Modified: 2012-04-07
Hi,

Can someone advise how I would cut up a number like 5000 or 281 in javascript and store into new variables.

thanks
0
Comment
Question by:jecommera
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37735388
what do you mean by cut up?
0
 

Author Comment

by:jecommera
ID: 37735424
So I will have

from 281, I get

a=2, b=8, c=1
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 125 total points
ID: 37735430
var val = 281;
var digits = new Array()
while (val > 0)
{
  digits[ digits.length ] = val%10;
  val = val /10;  
}

//now you can take the individual values out of digits array
0
 

Author Comment

by:jecommera
ID: 37735453
Can you please explain the code.
thanks for your help
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 37735459
i am simply removing the last digit the from variable in every iteration of the while loop and storing that last digit in the digits array

Kindly tell me if it works or not
0
 

Author Comment

by:jecommera
ID: 37735528
Yes it works - I just trying to understand your code here.

Why did you use 10 here?

digits[ digits.length ] = val%10;
  val = val /10;
0
 
LVL 10

Accepted Solution

by:
Gregg earned 125 total points
ID: 37735608
0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 125 total points
ID: 37735755
As an array:
var startingNumber = 281
var numArray = startingNumber.split('')

Open in new window


You can then put it into named variables if you want to... but this limits the ability to pass in different lengths of values (unless you want to make it super complicated).
0
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 125 total points
ID: 37736408
Strictly speaking this is what you want but consider using an array rather than individual variables:

var sample = 281;

var digit = sample.toString().match(/\d/g);

var index = -1;

while (++index != digit.length) {
    eval(String.fromCharCode(97 + index) + "=" + digit[index]);
}

alert("a = " + a + "\nb = " + b + "\nc = " + c);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now