Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This may sound like a very simple task but I can`t check every value on array to be 1. Either I am very tired and can`t see an obvious mistake or I didn`t know something about loops.
I have this code:
JavaScript
function longest (hour, val) {
 var result;
  for(var i = 0; i < hour.length; i++){
    if(hour[i].length === 1){result = "they are all 1 length"}
    else {result = "one or more of them is not 1"}
  }
  return result
}
longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'], 1)



So, it is dead simple, if every index is 1 length, print one message, if at least one of them is not, print another message.

What I have tried:

I thought the code above would work, however when I change length of some sting it still prints "they are all 1 length". Why? What am I missing?
I am a newbie, it is important to understand why for me.
Posted
Updated 11-Aug-16 16:42pm

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
AlexLearne 12-Aug-16 8:10am    
Yes, I think that would be very nice, do you use visual studio as well?
I hear people talk about debugging all the time but I don`t see many tutorials on them. Also, what about debugger in console, did it prove to be useful?
try this
JavaScript
 function longest(array, length) { // make use of length parameter, to check dynamic
           var result;
           var count = 0;
           for (var i = 0; i < array.length; i++)
               if (array[i].length === length)
                   count++; // increment the count if each item's length and length val is equal
           result = count == array.length ? 'they are all ' + length + ' length' : 'one or more of them is not ' + length;
// if count and input array length is equal then all the items matched the condition, else not

           return result;
       }

       longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'], 1)
 
Share this answer
 
function longest (hour, val) {
hour = hour || [];
var len = hour.length;
for(var i = 0; i < len; i++){
if(hour[i].length != 1)
return "they are all 1 length";
}
return "one or more of them is not 1";
}
longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'], 1)
 
Share this answer
 
better ... hit submit on that last one before reading what i wrote...

function longest (hour, val) {
hour = hour || [];
var len = hour.length;
for(var i = 0; i < len; i++){
if(hour[i].length != 1)
return "one or more of them is not 1";
}
return len > 0 ? "they are all 1 length" : "array is empty";
}
longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'], 1)
 
Share this answer
 
Comments
Patrice T 12-Aug-16 8:27am    
Do not repost solution
Use Improve solution instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900