javascript: unable to figure out the error!
Below is a script written in JavaScript, 开发者_高级运维the purpose is to take a sentence from input and that should be split into array of words eliminating white space. Every piece of word should be further split into 2 or 3 lettered depending on the size of word ignoring remaining letters in it.
If the said word is greater that 1 and less than 5 or if it is greater than 6 the it should be four letter word. The output of all words should be joined with an underscore in between.
Below is the code that is not working and I am unable to figure it out. Please guide me to make it work
document.getElementById("cliename").onchange = function(event){
var list_cname = new Array();
var cname = document.submit.cliename.value;
list_cname = cname.split(" ");
var list_len = list_cname.length;
var valu;
var final_valu;
var i=0;
for (i=0;i<=list_len;i++){
var sub_list = list_cname[i];
var sub_list_len = sub_list.length;
if(sub_list_len>=1 && sub_list_len<=5 ){
sub_list_len=sub_list_len-1;
}
else if(sub_list_len>=6 ){
sub_list_len=4;
}
var list_val = list_cname[i].substring(0,sub_list_len);
valu = list_val;
final_valu = valu + "_" + final_valu;
}
document.write("list -" + cname + ", len - " + list_len + ", final " + final_valu);
}
The error i get that is "SUB_LIST" is UNDEFINED.
For starters, change
for (i=0;i<=list_len;i++)
to
for (i=0;i<list_len;i++)
or list_cname[i]
will index out of bounds.
From a rough look at your code, without knowing the errors, etc..
You're referencing: sub_list_len
but that's not a length.
You would need to do something like:
sub_list_len.length
or
var sub_list_len = sub_list.length
I've made some changes to your code and put an example up on jsFiddle. Also pasted below
document.getElementById("cliename").onchange = function(event){
var list_cname = [];
var cname = this.value;
list_cname = cname.split(" ");
var list_len = list_cname.length;
var valu = "";
var final_valu = "";
var i=0;
for (i=0;i<=(list_len-1);i++)
{
var sub_list = list_cname[i];
var sub_list_len = sub_list.length;
if(sub_list_len > 1 && sub_list_len <= 5 )
{
sub_list_len = sub_list_len-1;
}
else if(sub_list_len>=6 )
{
sub_list_len = 4;
}
var list_val = list_cname[i].substring(0,sub_list_len);
valu = list_val;
final_valu = valu + "_" + final_valu;
}
document.write("list - \"" + cname + "\", len - " + list_len + ", final " + final_valu);
};
var list_cname = new Array();
var cname = document.getElementById('cliename').value;
list_cname = cname.split(" ");
var list_len = list_cname.length;
var valu;
var final_valu;
var i = 0;
for (i = 0; i <= list_len; i++) {
alert();
var sub_list = list_cname[i];
var sub_list_len = sub_list;
if (sub_list_len >= 1 && sub_list_len <= 5) {
sub_list_len = sub_list_len - 1;
} else if (sub_list_len >= 6) {
sub_list_len = 4;
}
var list_val = list_cname[i].substring(0, sub_list_len);
valu = list_val;
final_valu = valu + "_" + final_valu;
}
alert("list -" + cname + ", len - " + list_len + ", final " + final_valu);
<input type='text' id='cliename' value='1 2 3' />
精彩评论