Solved

how do I cut up a number like 281 in javascript

Posted on 2012-03-18
9
244 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now