Solved

modulo operation for all characters including non numbers

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html date format 9 20
null, truthy, falsey confusion 25 30
How to show popup no matter what a user clicks on navigation wise 10 32
Put javascript to get dropdown value 3 22
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
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…

756 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