Hi, i have done what the exercise has asked me to and passed it… But why is it relevant?.. like why would i want to know how many letters are in the sentence?
Seems like a reasonable question. I’ll try to give a reasonable answer. You might be hosting a message board, and have a character limit per post. This forum, for example has a minimum characters per post limit. As you get further along in learning to code, you’ll come to realize that you use the .length
property of different objects quite often.
Can the length property only be used to determine the length of a string? Or can it also be used to determine the length of other data types?
It is a property of all objects, though some will never report anything but, undefined
.
x = {a:1, b:2, c: 3, d: 4, e: 5}
{a: 1, b: 2, c: 3, d: 4, e: 5}
x.length
undefined
y = Array.from(Object.entries(x))
(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
0: (2) ["a", 1]
1: (2) ["b", 2]
2: (2) ["c", 3]
3: (2) ["d", 4]
4: (2) ["e", 5]
length: 5 //<<< note the physical property
__proto__: Array(0)
y.length
5
(y.length).length
undefined
Objects that have a length are generally iterable. Strings and arrays are iterable. Numbers, booleans, null, undefined, etc. are not.
Wouldn’t you use the minlength
and maxlength
attributes to achieve this? Could you please provide another example as to when you would use the length
property
You may have a text area where you adjust the font size of the text depending on how many characters there are to be displayed. There are many uses for the .length
property of different objects.
Here’s an example I just made up to count the number of pdf
files in an array of files. It uses the length of the array to iterate over each element of the array, and the length of each file to determine if the last 4 characters are ".pdf"
. This example is trivial, but we use .length
quite frequently in programming.
const my_files = ["blah.pdf", "blahblah.doc", "blablah.pdf", "blah.exe", "blahblahblah.pdf"];
const pdfCount = files => {
let count = 0;
for (let i = 0; i < files.length; i++) {
const currentFile = files[i];
if (currentFile.slice(currentFile.length - 4) == ".pdf") {
count++;
}
}
return count;
}
console.log(pdfCount(my_files));
Output:
3
You may or may not be familiar with the syntax and/or concepts used, but the .length
property is very useful.
Ok thanks for this example!
For the record, since no real need for indices exists, we can use for..of
and only read the array values.
const pdfCount = files => {
let count = 0
for (let name of files) {
x = name.slice(-4)
count += +(x === '.pdf')
}
return count
}
Mind this all runs contrary to the main question about length. That’s because String.slice()
doesn’t need to know the length. The negative index reads from the right.
Also note above the use of the unary operator, +
which casts (coerces) the booleans to their integer equivalents, 0 for false, 1 for true.
Perhaps this is a better example still using .length
.
const my_files = ["blah.pdf", "blahblah.doc", "blablah.pdf", "blah.exe", "blahblahblah.pdf"];
const pdfCount = files => files.filter(e => e.slice(-4) === ".pdf").length
console.log(pdfCount(my_files));
Indeed. Nicely done.
We’re still skirting the original question since .length
above refers to the array, not the sentence. At least above we were both working with strings.
Just ribbing you. We’re both guilty here of using foreign concepts, as new learners go. Hopefully this peek ahead is not too disruptive.
Yes. Hopefully not. Ideally, a peek at what’s possible with continued study.
In general, we might want to know the length of something, so we can decide how to fit it into a page or a box, or perhaps whether to trim off part of it before presenting it. This could apply to a sentence or to a list of items.
You said “you’ll come to realize that you use the ‘.length’ property of different objects quite often” but mtf said " ‘.length’ is a value, only". So which one is correct?
.length
is a property that has an associated value, an integer.
But how would the length property be used in creating video games. Please reply in simple words as I am still in school.
If length was a integer, how would you use it to display negative numbers?
Length is always positive or zero, never negative. Not sure I fully get what you mean.