MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / LinearSearch

Function LinearSearch

assets/js/linearsearch.js:32–84  ·  view source on GitHub ↗
(delay = 300)

Source from the content-addressed store, hash-verified

30
31// Asynchronous LinearSearch function
32async function LinearSearch(delay = 300) {
33var blocks = document.querySelectorAll(".block");
34var output = document.getElementById("text");
35
36//Extracting the value entered by the user
37var num = document.getElementById("fname").value;
38
39//Changing the color of all the blocks to voilet
40for (var i = 0; i < blocks.length; i += 1) {
41 blocks[i].style.backgroundColor = "#00ccff";
42}
43
44output.innerText = "";
45
46var flag = 0;
47// LinearSearch Algorithum
48for (var i = 0; i < blocks.length; i += 1) {
49 //Changing the color of current block to red
50 blocks[i].style.backgroundColor = "#FF4949";
51
52 // To wait for .1 sec
53 await new Promise((resolve) =>
54 setTimeout(() => {
55 resolve();
56 }, delay)
57 );
58
59 //Extracting the value of current block
60 var value = Number(blocks[i].childNodes[0].innerHTML);
61
62 // To compare block value with entered value
63 if (value == num) {
64 flag = 1;
65 output.innerText = "Element Found";
66 blocks[i].style.backgroundColor = "#13CE66";
67 break;
68 } else {
69 // Changing the color to the previous one
70 blocks[i].style.backgroundColor = "#00ccff";
71 }
72}
73//When element is not found in the array
74if (flag == 0) {
75 output.innerText = "Element Not Found";
76}
77// To enable the button "Generate New Array" after final(sorted)
78document.getElementById("Button1").disabled = false;
79document.getElementById("Button1").style.backgroundColor = "#6f459e";
80
81// To enable the button "Linear Search" after final(sorted)
82document.getElementById("Button2").disabled = false;
83document.getElementById("Button2").style.backgroundColor = "#6f459e";
84}
85
86// Calling generatearray function
87generatearray();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected