Solved

modulo operation for all characters including non numbers

Posted on 2013-11-14
6
294 Views
Last Modified: 2013-11-14
I have 87 boxes and I want to use JavaScript to somewhat evenly distrube the items into the boxes.

The modulo operation works well for this if all my items are labeled with a number:

alert('Put item "12345" into box number ' + 12345%87 );

How can I do this by text content?  Is there a way to convert characters into a number for this?

alert('Put item "zq7fgTpQ@bRh" into box number ' + "zq7fgTpQ@bRh"%87 );
0
Comment
Question by:hankknight
[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
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39648556
You could pull all boxes into an array and then target them by array index

box[0]
box[1]
box[2]
etc.

I tend to use jQuery these days, so something like this:

//get all inputs
var boxes = $('input');

$(boxes[0]).val("some value");
$(boxes[1]).val("some value");
$(boxes[2]).val("some value");

Open in new window

0
 
LVL 1

Expert Comment

by:mark_667
ID: 39648582
First you'd have to check if the text content contained any characters and if it did putting it through parseInt to convert it to a number.
http://www.javascripter.net/faq/convert2.htm

This will solve your immediate problem but it's a clumsy way of doing it. Wouldn't it be easier to assign each item an ID number and just use that to reference everything instead of parsing names?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39648671
check this to see if thta help  : http://jsfiddle.net/M27pK/
    var str = "zq7fgTpQ@bRh";
    for(var i=0;i<str.length;i++) {
        document.write(str.charCodeAt(i));
        document.write("<br/>");
    } 

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 16

Author Comment

by:hankknight
ID: 39648735
I like leakim971's idea and have modified it to meet my needs:
function modulochar(str, m) {
    var x = 0;
    for(var i=0;i<str.length;i++) {
        x=x+str.charCodeAt(i);
    } 
   return x%m;
}

alert('Put item "ay7fgzTpQ@pRh" into box number ' + modulochar('ay7fgzTpQ@pRh', 87) );

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39648742
If you want it to distribute them perfectly evenly, then you should just have an iterator that counts how many items and put one in each box in turn.

If you just want it to distribute them fairly evenly, then you can sum up the values of all the characters in the string and use modulo on that.
Starting with leakim's script
    var str = "zq7fgTpQ@bRh";
    int sum = 0;
    for(var i=0;i<str.length;i++) {
        sum += str.charCodeAt(i);
    } 
    return sum % 87;

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39648748
Oops, came in a hair late on that one I guess. Glad you got what you needed.
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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
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)
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

615 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