I have this exercise that already got the answer, but after hearing the explanations, still don't understand. This is the exercise:
"write a function isUniform() which takes an array as an argument and returns true if all elements in the array are identical"
This is the solution
function isUniform(numArr) {
var first = numArr[0];
for (var i = 1; i < numArr.length; i++) {
if (numArr[i] !== first) {
return false;
}
}
return true;
}
I got it almost right, but i did a else statement with the "return true" and it didn't work. Why does it work with the "return true" outside of the for loop? (edited) This is how i did the first time:
function isUniform(numArr) {
var first = numArr[0];
for (var i = 1; i < numArr.length; i++) {
if (numArr[i] !== first) {
return false;
}
else {
return true;
}
}
}
If you return true outside the loop, then it checks every element in the loop until one matches the if
test or it gets to the end of the loop.
If you return true inside the loop then it will always hit a return
statement for the first element and then stop the loop.
I got it almost right, but i did a else statement with the "return true" and it didn't work
The solution below would return the wrong results in some cases because all it does is find the first element within the array that's equal to first
variable and return true, even though it hasn't searched the entire array.
function isUniform(numArr) {
var first = numArr[0];
for (var i = 1; i < numArr.length; i++) {
if (numArr[i] !== first) {
return false;
}
else {
return true;
}
}
}
I have this exercise that already got the answer, but after hearing the explanations, still don't understand.
let's assume this is your array:
[10,10,13,10,10]
let's assume this is the variable first
:
first = 10;
The if statement
below which is within the for loop
basically says if the variable first ( 10 )
is not equal to the element at the current index i
( nth
number within the array) then return false
. This makes sense because if at this moment the variable first
is not the same with the element at the specified index
for example index 2 (number 13)
then there is no point to carry on. Hence, it will return
false.
if (numArr[i] !== first) {
return false;
}
now let's assume the array is:
[10,10,10,10,10]
let's assume this is the variable first
:
first = 10;
now the variable first
will be compared against each element
within the array and it says "is 10 not equal to the current element". in this case that's false because 10 is equal to 10
. This will propagate down the array and control will never pass inside the if block. Eventually, control passes down to the return
true statement.
if (numArr[i] !== first) {
return false;
}
It works because is the final statement in your function. Basically your function will return true if the condition inside for loop will not be triggered
Let's say you've got a broken printer which once in a while messes the printout. Now you printed 20 copies and want to know if every paper is fine. So now you would have to iteratively compare every copy until you found one which is not matching (and know it's time to get a new printer?). Or you've gone the way through the hole stack and know every copy is fine (and you've wasted time for nothing).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.