Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

modulo operation for all characters including non numbers

Posted on 2013-11-14
6
Medium Priority
?
297 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 44

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 2000 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
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!

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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.
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

715 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