# How do you console.log() in linear search algorithm?

How would you console.log(“Found”) and (“not Found”) based on linear search algorithm in JS?

``````//  we have number of items
const items = [2, 5, 3, 6, 7, 10, 34, 63, 20, 14];

// the number we r looking for
const item = 10;

function searchItem() {
//go over the number of items
for (let i = 0; i < items.length; i++) {
//if found,print found
if (items[i] == item) {
console.log(`Item is found, it is \${items[i]}`);
} else {
}
}
}

searchItem();
``````

Problem here is, it prints out the rest of number if I put it inside the loop or printout the “Nothing found” anyway at the end.
if I break out of loop, it will print out the rest of the “not found” until the item is found.

could you guys help me out on this?

If you find the item then `console.log` and use the `return` keyword to terminate the function. Only `console.log` at the end of the function rather than inside the loop.

Use some form of flag variable that exists outside of the loop. I would also print the index of the item, not the value, because the value is already known.

``````let flag = false;
let values = ["h", "i", "t"]
let target = "h"

function find(arr, t) {
let indexArr = []
for (let i = 0; i < values.length; i++) {
if (values[i] == target) {
console.log(`Item found at \${i}`)
flag = true;
}
}
return flag
}

let output = find(values, target)
``````

This approach can be optimized. If I wanted to break the loop instantly and not care about telling someone the index of the value, I could use the following:

``````function find(arr, t) {
let indexArr = []
for (let i = 0; i < values.length; i++) {
if (values[i] == target) {
return true
}
}
return false
}

let output = find(values, target)
``````

Notice how I don’t return “Item Found”, I instead return true or false. This makes it a more universal function for others to use. Strings should always be a last resort. Always try to simplify logic into Booleans or lists of pure data.

Instead of writing the following and possibly dealing with typos (etc, “Item FOund”)

``````if (output == "Item Found") { ...
``````

I could simply write the following that is universal

``````if (output) { ...
``````

You could also use a method that saves the index of every location of your target in the array. This would be a more efficient approach if the target is repeated, which is usually the case. This is also more useful because in a real world application, you are trying to find something and need to save the location of that item.

``````function find(arr, t) {
let indexArr = []
for (let i = 0; i < values.length; i++) {
if (values[i] == target) {
indexArr.push(i)
}
}
return indexArr
}

let output = find(values, target)
console.log(output)
if (output.length == 0) {
} else {
console.log(`Item Found`)
}
``````

On a side not, you also could just use `.indexOf()` which will return a index or -1 if the item is not found.

2 Likes

Only console.log at the end of the function will will print out that message anyway regardless of conditions.

Thank You So much! This is exactly what I was looking for! 1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.