Binary search algorithm in JavaScript

Size
1,798 Kb
Views
10,120

How do I make an binary search algorithm in javascript?

What is a binary search algorithm in javascript? How do you make a binary search algorithm in javascript? This script and codes were developed by Tomoyuki Kashiro on 19 January 2023, Thursday.

Binary search algorithm in JavaScript Previews

Binary search algorithm in JavaScript - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Binary search algorithm in JavaScript</title>
</head>
<body> <h1>Binary search algorithm in JavaScript</h1>
<p>Please check javascript code.</p> <script src="js/index.js"></script>
</body>
</html>

Binary search algorithm in JavaScript - Script Codes JS Codes

/*************************************** * search ***************************************/
var binarySearch = function(list, target){ var middleIndex, middle, result = 'nothing'; while(list.length > 0){ var middleIndex = Math.floor(list.length/2); var middle = list[middleIndex]; if(middle === target){ result = target; break; } if(middle > target){ list = list.slice(0,middleIndex); }else{ list = list.slice(middleIndex+1); } } return result;
};
/*************************************** * search using closure ***************************************/
//var binarySearch = function(list, target){
// var result;
//
// (function search(list, target){
// var middleIndex = Math.floor(list.length/2);
// var middle = list[middleIndex];
//
// if(middle === target){
// result = target;
// return;
// }
//
// if(middle > target){
// list = list.slice(0,middleIndex);
// }else{
// list = list.slice(middleIndex+1);
// }
//
// if(list.length === 0){
// result = 'nothing';
// }else{
// search(list, target);
// }
//
// })(list, target);
//
// return result;
//};
/*************************************** * main ***************************************/
var list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19], target = 15, result = binarySearch(list, target);
console.log(list);
console.log('index of tartget(' + target + ') is ' + result);
Binary search algorithm in JavaScript - Script Codes
Binary search algorithm in JavaScript - Script Codes
Home Page Home
Developer Tomoyuki Kashiro
Username Tkashiro
Uploaded January 19, 2023
Rating 3
Size 1,798 Kb
Views 10,120
Do you need developer help for Binary search algorithm in JavaScript?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Tomoyuki Kashiro (Tkashiro) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!