Java JavaScript arrays and loops combination question

AI Thread Summary
The discussion centers on improving a JavaScript function designed to check if a number exists within a predefined array of lost numbers. Key points include the suggestion to modify the for loop's increment to either "i = i + 1" or "i++" for proper iteration. Additionally, it's recommended to return boolean values (true or false) instead of their string equivalents for accuracy. The use of the array method "indexOf" is also mentioned as an alternative for checking the presence of a number in the array. Proper variable declarations for "i" and "n" are emphasized to ensure the function operates correctly.
mindauggas
Messages
127
Reaction score
0
var lost = [4, 8, 15, 16, 23, 42];
var count = lost.length;

var isLost = function (n) {
for (i=lost[0]; i<count; i=lost[i+1]) {
if ( n === lost) {
return "true";
}
}
return "false";
};

is it possible to in corporate array positions into the for loop? (i)
do I even begin to approach the correct "incorporation"? (ii)
 
Technology news on Phys.org
As I understand your questions:
(i) Use i = i+1, or i++ as the increment.
(ii) You would want to return the boolean literals true or false instead of their string representation. You may also want to check out the array indexOf operation [1].

[1] http://www.w3schools.com/jsref/jsref_indexof_array.asp
 
You should do something like this to read the contents of an array. Don't forget to declare var i and n (whatever that is):
Code:
for(var i = 0; i < lost.length; i++){
    if(n == lost[i]){
        return true;
    }
}
mindauggas said:
is it possible to in corporate array positions into the for loop? (i)
do I even begin to approach the correct "incorporation"? (ii)
var i in the for loop is the position of the array that you are examining.
 
Thanks.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
4
Views
1K
Replies
9
Views
2K
Replies
10
Views
3K
Replies
13
Views
4K
Replies
8
Views
6K
Replies
3
Views
5K
Replies
14
Views
4K
Back
Top