Solved

modulo operation for all characters including non numbers

Posted on 2013-11-14
6
272 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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Article by: Nicole
This is a research brief on the potential colonization of humans on Mars.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

823 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