Solved

how do I cut up a number like 281 in javascript

Posted on 2012-03-18
9
250 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: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
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!

 

Author Comment

by:jecommera
ID: 37735453
Can you please explain the code.
thanks for your help
0
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

759 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