2

I have this script here http://jsfiddle.net/slashingweapon/7JmGn/

$(function () {
var wordCounts = {};
$("input[type='text']:not(:disabled)").keyup(function () {
    var matches = this.value.trim().split(/\s+/);
    wordCounts[this.id] = matches ? matches.length : 0;
    var finalCount = 0;
    var x = 0;
    $('input:checkbox:checked').each(function () {
        x += parseInt(this.value);
    });
    x = (x == 0) ? 1 : x;
    $.each(wordCounts, function (k, v) {
        finalCount += v * x;
    });
    $('#finalcount').val(finalCount)
}).keyup();
$('input:checkbox').change(function () {
    $('input[type="text"]:not(:disabled)').trigger('keyup');
});

It is supposed to count the words, and change the value on the right depending if one or more of the checkboxes are checked.

I have 2 issues with it, first is how do i make it display 0 initially instead of 1.

My second issue with it, is that i want it to ignore punctuation and words under specific length (2-3), such as "as,in,no".

EDIT: First issue is solved, thanks to "Karna", but his solution for length does not seem to work for me : http://jsfiddle.net/y4Bp5/1/

Instead it ignores only the first input before space, if its under specific length

EDIT 2: "Rummap Datta"'s solution works, solved.

Konata
  • 275
  • 1
  • 3
  • 14
  • Please have a look at http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior – Ingo Bürk Apr 18 '14 at 09:25

2 Answers2

4

Change

   x = (x == 0) ? 1 : x;  

to

   x = (x == 0) ? 0 : x;

this will set 0 as default value

You can add check for this.value.length to ignore word with particular length

 $("input[type='text']:not(:disabled)").keyup(function () {
        if(this.value.length > 2){
           var matches = this.value.trim().split(/\s+/);
           // Rest of the code
        }  
 });

EDIT:
Updating the default value to 0 will make the result as 0 always at it multies the x with the input word lenght
instead you can do

 $("input[type='text']:not(:disabled)").keyup(function () {
        //.....    
         x = (x == 0) ? 1 : x;  
        //......
        if(x==1){
           $('#finalcount').val(0);
        }else{
           $('#finalcount').val(finalCount);
        }   
    }).keyup();
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • 3
    What does this x = (x == 0) ? 0 : x; do? I mean to say when the value of x is 0, the condition is true and you assign 0 to x. Why do you need this statement at all? – Rupam Datta Apr 18 '14 at 09:18
  • @RupamDatta The actual code was written to avoid assigning 0 to x. I have updated my answer – Ajinkya Apr 18 '14 at 09:23
  • @Karma He is right, though, you can omit the line `x = (x == 0) ? 0 : x` entirely now, it serves no purpose. – Ingo Bürk Apr 18 '14 at 09:25
  • @IngoBürk: Yes agreed, but the code required some more modifications. I have updated my answer – Ajinkya Apr 18 '14 at 09:27
  • The length solution only ignores the first input if its under 2, if I enter things after first space that are under 2 they will be counted and not ingonerd – Konata Apr 18 '14 at 09:51
  • @Konata: You can check words in `matches`, compare them with desired words and ignore if required. – Ajinkya Apr 18 '14 at 10:05
  • Im a js noobie, how could i do that? – Konata Apr 18 '14 at 10:13
2

Can you try this?

As per my understanding of the question I've changed a few lines of code. Please check.

 $('input:checkbox:checked').each(function () {     
     x += parseInt(this.value);
 });

 $.each(wordCounts, function (k, v) {
     finalCount=(matches==0)? 0 : (finalCount + (v * x));
 });
 $('#finalcount').val(finalCount);

FIDDLE

Few more changes to add solution to the second problem. Please check if it helps.

tempCount=0;
    for(i=0;i<matches.length;i++){
        if(matches[i].length>2){
            tempCount++;
        }
    }

    finalCount=tempCount*x;

    $('#finalcount').val(finalCount);

FIDDLE 2

Rupam Datta
  • 1,849
  • 1
  • 21
  • 36
  • It doesn't display the number if only 1 word is entered, and does not solve the ignore specific length problem, but thank you for the effort – Konata Apr 18 '14 at 09:47
  • Solves my first issue, thank you, but poster above you fixed that already, my current problem is to make the word count ignore entries under 3 characters, if you check my original post i made an edit, and attacked a fiddle – Konata Apr 18 '14 at 10:04
  • -1: If you don't know if or how it works, don't post it. – Ingo Bürk Apr 18 '14 at 10:36
  • @IngoBürk Rephrased the post. – Rupam Datta Apr 18 '14 at 10:41