search
HomeWeb Front-endJS TutorialCommon errors of '/' operator in javascript_javascript tips

//Forgetful recursive version of binary search

Copy code The code is as follows:

function binary_search(arr, target,low,high){
if(low    var min=(low high)/2;
if(target>arr[min])
return binary_search(arr,target,min 1,high);
else
return binary_search(arr,target,low,min);
}else if(low==high){ //only There is one element left
if(arr[low]==target)
return low;
else return -1;
}else if(low>high){ //Empty, use arr .length-1 should be considered when calculating the initial high of arr.
return -1;
}
}

var arr=[1,2,3,4, 5,6];
alert(binary_search(arr,3,0,arr.length-1));
Looking at the data structure at night, I wrote a binary search algorithm in js (the code is as above), and then I randomly wrote an array as the test data (as above). According to the idea, it should output the subscript of the search target, but something unexpected happened. I saw the CPU spinning wildly for an instant. After about two seconds, the browser automatically terminated the script. running, and then I felt puzzled for a while.

According to experience, there should be an infinite loop in the process of running the script. I looked at the algorithm during self-study and found no problems (you can just enter the code directly according to the textbook and you will not be wrong) , but the problem remains. So I added an output statement to the first judgment condition, as follows:
Copy code The code is as follows:

//Binary search forgetful recursive version function binary_search(arr,target,low,high){
if(low var min=(low high )/2;
 if(target>arr[min])
return binary_search(arr,target,min 1,high);
else
return binary_search(arr,target,low,min) ;
}else if(low==high){ //Only one element left
if(arr[low]==target)
return low;
else return -1;
}else if(low>high){ //Empty, it should be considered when using arr.length-1 to calculate the initial high of arr
return -1;
}
}

Run it, and a dialog box will pop up with the number 2.5~~ Suddenly, I suddenly have the urge to smash the computer.

Cause of error and summary:
The "/" operator in JavaScript is different from the "/" operator in C. The latter is automatically rounded, while the former will get a decimal if it is not divided by integers (for example 5/2=2.5).
Solution:

(1)var min=parseInt((low high)/2);
(2)var min=Match.floor((low high)/2);
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
c语言开根号运算符是什么c语言开根号运算符是什么Mar 06, 2023 pm 02:39 PM

在c语言中,没有开根号运算符,开根号使用的是内置函数“sqrt()”,使用语法“sqrt(数值x)”;例如“sqrt(4)”,就是对4进行平方根运算,结果为2。sqrt()是c语言内置的开根号运算函数,其运算结果是函数变量的算术平方根;该函数既不能运算负数值,也不能输出虚数结果。

Java中的%是什么意思Java中的%是什么意思Mar 06, 2023 pm 04:48 PM

在Java中,“%”是取余的意思,是一个二元算术运算符,可进行除法运算并获取余数,语法“操作数1 % 操作数2”。取余运算符“%”的操作数通常是正整数也可以是负数甚至是浮点数,如果负数参与此运算,则结果的正负取决于前面一个数是正数还是负数。

golang 报错:“invalid use of … operator” 如何解决?golang 报错:“invalid use of … operator” 如何解决?Jun 24, 2023 pm 05:54 PM

对于Golang开发者来说,“invaliduseof…operator”是一个常见的报错。这个报错通常会在使用变长参数函数时出现。它在编译时就会被检测出来,并指出哪些部分有问题。这篇文章将介绍如何解决这个报错。一、什么是变长参数函数变长参数函数也被称为可变参数函数,是Golang语言中的一种函数类型。使用变长参数函数可以像如下方式定义多个

php中“==”符号的含义是什么php中“==”符号的含义是什么Mar 14, 2023 pm 07:05 PM

在php中,“==”符号是一个比较运算符,可以比较两个操作数是否相等,语法“操作数1 == 操作数2”。“==”运算符会比较、并测试左边的变量(表达式或常量)是否与右边的变量(表达式或常量)具有相同的值;它只比较变量的值,而不是数据类型。如果两个值相同,则返回true值;如果两个值不相同,则返回false值。

php怎么判断两个数能否整除php怎么判断两个数能否整除Jan 10, 2023 pm 03:12 PM

在php中,可以使用“%”和“==”运算符来判断两个数能否整除;只需要使用“%”运算符将两个数相除获取余数,再使用“==”运算符判断获取的余数是否为0即可,语法“数1 % 数2 == 0”,如果为0则能整除,如果不为0则不能整除。

Python中的魔法方法Python中的魔法方法Apr 13, 2023 am 10:25 AM

python中的魔法方法是一些可以让你对类添加“魔法”的特殊方法,它们经常是两个下划线包围来命名的。Python的魔法方法,也称为dunder(双下划线)方法。大多数的时候,我们将它们用于简单的事情,例如构造函数(init)、字符串表示(str, repr)或算术运算符(add/mul)。其实还有许多你可能没有听说过的但是却很好用的方法,在这篇文章中,我们将整理这些魔法方法!迭代器的大小我们都知道__len__方法,可以用它在容器类上实现len()函数。但是,如果您想获取实现迭代器的类对象的长度

go语言运算符优先级怎么排序go语言运算符优先级怎么排序Dec 21, 2023 pm 05:03 PM

在Go语言中,运算符按照优先级从高到低的顺序进行计算。常见的运算符的优先级顺序:1、括号:()(最高优先级,用于强制改变运算顺序);2、​单目运算符;3、乘性运算符;4、加性运算符;5、移位运算符;6、按位运算符;7、比较运算符;8、逻辑运算符;9、条件运算符(三元运算符);10、赋值运算符等等。

JavaScript的位左移(<<)运算符是什么?JavaScript的位左移(<<)运算符是什么?Sep 13, 2023 pm 12:37 PM

使用按位左移运算符,从右侧移动一个或多个零位。不考虑最左边的位。示例您可以尝试运行以下代码来了解如何使用JavaScript按位左移运算符。<!DOCTYPEhtml><html>&nbsp;&nbsp;<body>&nbsp;&nbsp;&nbsp;<script>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write("B

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)