Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do I cut up a number like 281 in javascript

Posted on 2012-03-18
9
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jecommera
ID: 37735453
Can you please explain the code.
thanks for your help
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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 500 total points
ID: 37735608
0
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 500 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

715 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