1

I just opened the algorithms unlocked book and I am trying to implement the pseudocode they have for a linear-search in the book in Javascript.

Here's my code

var answer = 'not found';

function LinearSearch(A,n,searchQuery) {
  for (var i = 0; i < A.length; i++) {
    if A[i] === value {
      answer = i;
    }
  }
}

var names = ["Jack", "Molly", "Tristan", "Jacob", "Steph"]

console.log(LinearSearch(names, names.length, "Jacob"));

I am wondering what I am doing wrong here.

1
  • missing () for if condition and you are not returning any value from function Commented Jun 21, 2016 at 12:16

2 Answers 2

1

There were several issues with code in your question:

  • value was not defined, most probably you wanted to use searchQuery
  • missing parenthesis on if A[i] === value (a syntax errors)
  • n was never used (so I have removed in the code below)
  • missing return statement

    function linearSearch(data, searchQuery) {
      var answer = 'not found';
      for (var i = 0, len = data.length; i < len; i++) {
        if (data[i] === searchQuery) {
          answer = i;
        }
      }
      return answer;
    }

    var names = ["Jack", "Molly", "Tristan", "Jacob", "Steph"]
    var result = linearSearch(names, "Jacob");
    console.log(result);

Alternative you can use indexOf() as well posted by John. Or you could using a more exotic approach using findIndex().

findIndex() is particularly useful when you have an array of objects and need to get the index my and object property. You can also use the same result using using forEach().

Example using findIndex():

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

function findIndexByName(data, name) {
  var result = data.findIndex(function(element, index, array) {
    if (element=== name) {
	    return true;
    }
    return false;
  });
  return result;
}
var data = ["Jack", "Molly", "Tristan", "Jacob", "Steph"];
var result = findIndexByName(data, "Jacob");
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var answer = 'not found';

function LinearSearch(A,n,searchQuery) {
  for (var i = 0; i < n; i++) {
    if( A[i] === searchQuery) {
      return i 
    }
  }
  return answer
}

var names = ["Jack", "Molly", "Tristan", "Jacob", "Steph"]

console.log(LinearSearch(names, names.length, "Jacob"));

Syntax errors:

Line 5: change if A[i] === value to if( A[i] === searchQuery)

a function must return somthing.

EDit

you can use indexOf() method for short code.

var names = ["Jack", "Molly", "Tristan", "Jacob", "Steph"]
function LinearSearch(name){
  return names.indexOf(name) !== -1 ? names.indexOf(name) : 'not found'
}
console.log(LinearSearch("Tristan"))

2 Comments

How come you only did 'return i'? Does the function stop executing once the if statement is true and skips over the return 'answer' line?
answer = i; return answer; its the same as return i ( the second one better ), and yeah once the loop find the name it return the index ( i ) then the function stop runing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.