Solved

modulo operation for all characters including non numbers

Posted on 2013-11-14
6
252 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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Author Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

We are taking giant steps in technological advances in the field of wireless telephony. At just 10 years since the advent of smartphones, it is crucial to examine the benefits and disadvantages that have been report to us.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

728 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

12 Experts available now in Live!

Get 1:1 Help Now