linear search js
// arr.indexOf(val)
function linearSearchIndexOf(arr, val) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === val) {
return i;
}
}
return -1;
}
// arr.includes(val)
function linearSearchIncludes(arr, val) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === val) {
return true;
}
}
return false;
}
// arr.find(cb)
// cb = CallBack Function
function linearSearchFind(arr, cb) {
for (let i = 0; i < arr.length; i++) {
if (cb(arr[i]) === true) {
return arr[i];
}
}
return undefined;
}function searchAlgorithm (number){
for(let i = 0; i< number; i++){
if(number == array[i]){
console.log("Found it, it's at index " + i)
}else{
console.log("Not found")
}
};
}
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
searchAlgorithm(5)
Also in JavaScript:
- javascript string replace
- how run dockerfile
- useRef
- string length jquery
- Get node value in XML using jQuery
- how to run a vue js hello world app in vue version 3
- transfer data from one component to another angular
- difference between e.preventdefault and e.stoppropagation and return false
- regex for number and letters
- ckeditor get content html
- how to pass a value to a react funtion without immediately firing it
- remove square brackets from string javascript
- javascript remove object from array
- useref react
- javascript get elements that exist in two arrays
- get platform node
- installeer alle nodes in node red
- javascript base64 decode
- checking ascii in js
- convert json to 2d array
- react google maps get map center
- select class with data attribute jquery
- get css value jquery
- node js get file name without extension