lucy
lucy

Reputation: 9

counts the characters when reached at some point

I have the following code which is working fine;

var text_max = 459;
    var text_used = 0;
    var sms_count = 0;
    $('#leftstring').html(text_max + '');
    $('#usedstring').html(text_used + '');
    $('#sms_count').html(sms_count + '');
    $(document).on("keyup","#Message", function() {
        var text_length = $('#Message').val().length;
        var text_remaining = text_max - text_length;
        var text_completed = text_used + text_length;
        if(text_length <= 152) {
            var smscounts =+ parseInt(smscounts)+1;
        }
        $('#leftstring').html(text_remaining + '');
        $('#usedstring').html(text_completed + '');
        $('#sms_count').html(smscounts + '');
    });

Issue is with the sms_count as what i am trying is: i have a limit of 459 characters, so i want to divide it by 3, so when the characters reach 152, it should count to 1 and then for next 152 it should count 2 and then for last 152 it should count to 3

Upvotes: 0

Views: 44

Answers (4)

Carsten Massmann
Carsten Massmann

Reputation: 28196

This works:

var sms=152,text_max=3*sms; // good idea from caliburn, thanx ;-)
$(document).on("keyup","#Message", function() {
  var text_length = $('#Message').val().length;
  $('#leftstring').html((text_max - text_length) + '');
  $('#usedstring').html((text_length % sms) + '');
  $('#sms_count').html(Math.ceil(text_length / sms) + '');
});

see here: http://jsfiddle.net/fkgpwhb5/

Upvotes: 0

Ed Ballot
Ed Ballot

Reputation: 3485

You want this

smscounts = Math.ceil(text_length / 152);

Math.ceil will round up to the nearest integer. Since JavaScript division is a little wonky, you might have to do this instead

smscounts = Math.floor(text_length / 152) + 1;

Upvotes: 0

Jon Allen
Jon Allen

Reputation: 335

You could probably use Math.ceil() to find the minimum number of messages that would have to be sent.

var per_text = 152

$(document).on("keyup","#Message", function() {
    var text_length = $('#Message').val().length;
    var text_remaining = text_max - text_length;
    var text_completed = text_used + text_length;
    var smscounts = Math.ceil(text_length / per_text);

    $('#leftstring').html(text_remaining + '');
    $('#usedstring').html(text_completed + '');
    $('#sms_count').html(smscounts + '');
});

Upvotes: 1

LCIII
LCIII

Reputation: 3626

This should logistically accomplish what you stated:

    ...
    if(text_length <= 152) {
        var smscounts =+ parseInt(smscounts)+1;
    } else if (text_length <= 304) {
      //do logic
    } else { // it's > 304
      //do logic
    }
    ...

Upvotes: 0

Related Questions