Javascript 为什么按索引插入节点的方法不起作用?

Javascript 为什么按索引插入节点的方法不起作用?,javascript,Javascript,我想使用add()方法按索引插入一个节点,但它无法帮助修复它 这是通话的样子: list.add ('2', 1); // At the list: '1', '2', '3' throw new Error("Position Node Doesnt Exist!") 但当我打电话时会触发一个错误: list.add ('2', 1); // At the list: '1', '2', '3' throw new Error("Position Node Doesnt Exist!")

我想使用
add()
方法按索引插入一个节点,但它无法帮助修复它

这是通话的样子:

list.add ('2', 1); // At the list: '1', '2', '3'
throw new Error("Position Node Doesnt Exist!")
但当我打电话时会触发一个错误:

list.add ('2', 1); // At the list: '1', '2', '3'
throw new Error("Position Node Doesnt Exist!")
我的所有代码:
类节点{
构造函数(值){
this.value=value;//节点中的值
this.next=null;//链接到下一个节点
}
}
类单链接列表{
构造函数(){
/*指向结头的指针
(第一个元素)*/
this.head=null;
/*指向节点尾部的指针
(最后一项)*/
this.tail=null;
这个长度=0;
}
//插入到开头(返回插入的值)
addFirst(值){
//创建一个新节点
设newNode=newNode(值);
//处理链表不为空时的大小写
如果(这个标题){
newNode.next=this.head;
//this.head=newNode;
}else{//链接列表为空
this.tail=newNode;
//this.head=newNode
}
//将头部设置为新节点
this.head=newNode;
//增量计数
这个.length++;
}
//插入到末尾(返回插入的值)
addLast(值){
//创建一个新节点
设newNode=newNode(值);
//处理链表不为空时的大小写
if(this.tail){
this.tail.next=newNode;
//this.tail=newNode;
}否则{
this.head=newNode;
//this.tail=newNode
}
this.tail=newNode;
//增量计数
这个.length++;
}
findIndex(项目){
让currentNode=this.head;
while(当前节点){
if(currentNode.value==项){
返回当前节点;
}
currentNode=currentNode.next;
}
返回null;
}
添加(值、索引){
//根据传递的名称创建新节点
设newNode=newNode(值);
//找到要插入的位置或项目节点。
让positionNode=this.findIndex(索引);
if(positionNode!=null){
//首先将新节点的下一个指针设置为下一个位置节点的指针
newNode.next=位置node.next;
//最后,将positionNode的next更新为新节点
positionNode.next=newNode;
这个.length++;
}否则{
//未找到位置,返回错误
抛出新错误(“位置节点不存在!”)
}
}
}
let list=new SinglyLinkedList();
列表。addFirst('1');
list.addLast('3');
列表。添加('2',1);

控制台日志(列表)您正在尝试在
findIndex
方法中将字符串与数字进行比较:

if (currentNode.value === item) {
要允许自动类型强制,可以使用双相等运算符。或者可以将两个值转换为相同的类型(数字或字符串)

请在下面找到已编辑的工作代码段

类节点{
构造函数(值){
this.value=value;//节点中的值
this.next=null;//链接到下一个节点
}
}
类单链接列表{
构造函数(){
/*指向结头的指针
(第一个元素)*/
this.head=null;
/*指向节点尾部的指针
(最后一项)*/
this.tail=null;
这个长度=0;
}
//插入到开头(返回插入的值)
addFirst(值){
//创建一个新节点
设newNode=newNode(值);
//处理链表不为空时的大小写
如果(这个标题){
newNode.next=this.head;
//this.head=newNode;
}else{//链接列表为空
this.tail=newNode;
//this.head=newNode
}
//将头部设置为新节点
this.head=newNode;
//增量计数
这个.length++;
}
//插入到末尾(返回插入的值)
addLast(值){
//创建一个新节点
设newNode=newNode(值);
//处理链表不为空时的大小写
if(this.tail){
this.tail.next=newNode;
//this.tail=newNode;
}否则{
this.head=newNode;
//this.tail=newNode
}
this.tail=newNode;
//增量计数
这个.length++;
}
findIndex(项目){
让currentNode=this.head;
while(当前节点){
如果(currentNode.value==项){
返回当前节点;
}
currentNode=currentNode.next;
}
返回null;
}
添加(值、索引){
//根据传递的名称创建新节点
设newNode=newNode(值);
//找到要插入的位置或项目节点。
调试器;
让positionNode=this.findIndex(索引);
if(positionNode!=null){
//首先将新节点的下一个指针设置为下一个位置节点的指针
newNode.next=位置node.next;
//最后,将positionNode的next更新为新节点
positionNode.next=newNode;
这个.length++;
}否则{
//未找到位置,返回错误
抛出新错误(“位置节点不存在!”)
}
}
}
let list=new SinglyLinkedList();
列表。addFirst('1');
list.addLast('3');
列表。添加('2',1);

控制台日志(列表)您正在尝试在
findIndex
方法中将字符串与数字进行比较:

if (currentNode.value === item) {
要允许自动类型强制,可以使用双相等运算符。或者可以将两个值转换为相同的类型(数字或字符串)

请在下面找到已编辑的工作代码段

类节点{
构造函数(值){
这就是价值