you should check the array length otherwise undefined index will be generated