JS .length property


#1

I wish to read the length of an integer value INSIDE an array, so what can I do? considering the .length property would - in this case - read the number of elements in the array so I can’t really use that


#2

you can’t use .length property on integer, some good solutions are suggested here:

https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript

if you want the number digits of integer for any reason


#3

So will the following solution work for me, considering I need to read the number of digits in every element in an array: (including some extra info to help u know what’s happening)

var herd = window.prompt('Enter the number of cows in the herd', '');
var IDofCows = [];
for (var i=0; i<herd; i++) {
  do {
    IDofCows.push(i)= window.prompt('Enter the three digit ID code of the cow', '');
    var length = Math.log10(IDofCows[i]);
  } while ( length = 3 )
}

Will this make sure only three digit codes from 001 to 999 are entered? In case you’re wondering, it’s a school assignment


#4

this one:

Math.log(number) * Math.LOG10E + 1 | 0; 

seems to work fine. where number is the variable containing the result

also, here:

IDofCows.push(i)= window.prompt('Enter the three digit ID code of the cow', '');

you can’t push to an array and assign to a variable on the same line, besides why would you want to push i? i thought you wanted to push the three digit ID of cow

first store the three digit in a variable, then verify its 3 digits, then push it to array


#5

i goes from 0 to 100 or something so wont it be the specific address of the element i want to push the input value (inputed via window prompt) to that position? I’m sure I’m forgetting something, let me revise my notes lol


#6

Nvm that, I just checked and realized that push would push the value to the end of the array and I can write window prompt inside the push() function then?


#7

also here:

while ( length = 3 )

a single equal sign means assign, is that what you want to do here?

i wouldn’t do that, what if the user doesn’t enter 3 digits? If you already pushed to the array, you would have to remove it again. First verify that the user input is vaild before pushing to the array

.push() is indeed appending/inserting at the end of array.


#8

Alright then I will use

while ( length === 3 ) {
.
.
.
}


#9

do while is fine, but you have to use the right amount of equal signs. Why would you switch while all of a sudden?


#10

I misread your reply, you actually meant I should first verify that the length is 3 digits and then push it in, irregardless I get it now and I guess I’ll use do-while then


#11

but verifying that the user input is valid requires more changes then just using a do while loop


#12

I understand that, I will make the necessary changes
Inside the do-while loop I will declare a variable, put it equal to the window.prompt that I use for input and then I will find its length, if its length is 3, the do-while loop will let it thru and then I will put the value into the array - outside the do-while loop, does that sound right?


#13

That sounds right :slight_smile:


#14

Also would it make sense to use parsefloat() on the input before finding the length of the input? Just in case it’s a string? To my understanding parsefloat() extracts the integer values from a string and represents it as a number


#15

prompt seems to handle this by default, but can’t find much on its behaviour

then use parseInt, why would you want a float value?


#16

Wait nvm, I think parseInt will suit my needs better, didn’t exactly know what a float value was till you asked me n I googled so now I think I know the difference between parseInt and parsefloat as well