What is the reason why using slice(0,-1) does not remove just one element from an array

I am facing an issue with deleting the last number in a string. When I try using .pop, it removes all instances of the number. For example, if my array is ["12","+","12"], using .pop will remove both 12s when I only need to remove one.

quene.slice(0,-1)

https://i.sstatic.net/x2oZM.png Pressing CE is not working for me https://i.sstatic.net/MFiJs.png

Answer №1

It is important to first assess the length of a string before proceeding with any actions.

// Verify if the length of the last element is greater than 1
if(queue[queue.length - 1].length > 1){
  // Trim the last character and update the string
  queue[queue.length - 1] = queue[queue.length - 1].slice(1, -1);
} else {
  // If not, remove the last element
  queue.pop();
}

In an alternative approach, you can remove the last element, evaluate if its string length exceeds 1, then trim the last character and add it back to the array.

// Extract the last element from the array
let last = quene.pop();

// Check if the element's length is greater than 1
if(last.length > 1){
  // Remove the final character from the string and reintroduce it to the array
  queue.push(last.slice(1, -1))
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Structure of directories in NodeJS

I'm diving into the world of web development and NodeJS, embarking on the task of streamlining our app's code and directory layout. Seeking the insights of seasoned developers, I am keen to gather opinions on the file structure pattern I am cons ...

Define a new type that represents an array of integers

Currently, I am delving into an implementation of code that utilizes a typedef to a double array, along with pointers. The intricacies of the code and understanding how it functions, as well as comprehending the variable types and their relationships, have ...

Add a trash can or delete icon within every row of a table using Vue.js

I am new to vue.js and I'm struggling to implement a trash icon in each row of a table for deleting rows. Additionally, I'm trying to make the input of a cell act as a dropdown menu or list within the table rows. I recently came across this scrip ...

What is the best way to create an object using a string value as a reference?

I am seeking a way to efficiently utilize a string within an API factory to dynamically create the appropriate object from a class. Here is the script: import StoryApiService from './story' import AssignmentApiService from './assignment&apo ...

How to rotate an object in Threejs using the mouse without having to click and drag?

I'm searching for a solution to rotate around an object in Threejs without the need to hold down the mouse button. A good example can be found on this website: which utilizes Threejs. Despite searching through forums and documentation, I have been un ...

What sets Vuex apart from the Event Bus mechanism?

In my quest to grasp the intricacies of component communication, a question has surfaced: What sets Vue's event bus strategy apart from Vuex in handling inter-component communication? Additionally, when is the optimal moment to employ each technique a ...

Establish the starting status for material-ui dialog

In my MaterialUI dialog, there are various elements like text fields and drop downs that require specific values set each time the dialog is opened. Some elements also need to be loaded based on certain conditions being met, such as loading user data. I a ...

Change a TypeScript alias within the @types namespace

While using Typescript 3, I encountered a situation where I needed to modify a type alias from the @types/json-schema definition provided by DefinitelyTyped. The issue arose when I wanted to incorporate a custom schema type into my code. Since this custom ...

What could be the reason for encountering an error when calling await on a function that has the async

In my node+ express application, I am facing an issue with a controller method that is performing an asynchronous operation. Despite declaring the method with the async modifier, it is throwing a SyntaxError: await is only valid in async functions and the ...

The Arrow notations don't seem to be functioning properly in Internet Explorer

Check out my code snippet in this JSFiddle link. It's working smoothly on Chrome and Mozilla, but encountering issues on IE due to arrow notations. The problem lies within the arrow notations that are not supported on IE platform. Here is the specifi ...

What are the best strategies for eliminating element cloning and duplication in Vue?

As a novice programmer, I've developed a web app similar to Trello. It allows users to create boards and within those boards, lists can be created. Each list is displayed uniquely with different IDs. However, the list items are displayed with the same ...

The element div is not permitted as a child of the element h5 in this particular scenario

My code snippet is as follows: $compile .= "<h5 data-count='".$acctemmpi. "' class='shortcode_accordion_item_title expanded_". $expanded_state . "'>" . $title . "<div class='ico'&g ...

Is it considered a poor practice to self-instantiate within a static method of a JavaScript class

Do you think this object-oriented JavaScript (TypeScript) code is not well-written? class KYC { public reference; public data = null; constructor(id: string) { this.reference = id ? firestoreAdmin.collection('kyc').doc(id) : fi ...

Unable to access property value in React component

Hello everyone! This is my first time posting here, so I hope I'm explaining this correctly. I've been working on a React multi-step form, and I'm trying to figure out how to display a dynamic number of input fields in one of the steps. The ...

Tips for customizing bootstrap's fixed navbar-default to ensure that the list items align downwards:

Is it possible to customize the bootstrap fixed navbar-default so that the li elements align downward instead of at the top? If you have any corrections or custom solutions, I'd love to see them! Feel free to share your code on platforms like CodePen, ...

js: Is there a more efficient method for looping through a deeply nested object?

If I need to retrieve a list of just the types of cars from this dataset: let vehicles = [ { magnet: [ true ], cars: [ { type: "BMW" } ], name ...

Ensure that the cursor is consistently positioned at the end within a contenteditable div

I'm working on a project where I need to always set the caret position at the end of the text. By default, this works fine but when dynamically adding text, the caret position changes to the starting point in Chrome and Firefox (Internet Explorer is s ...

Arranging strings in alphabetical order using Groovy

I've recently been delving into the world of arrays in Groovy. I'm currently figuring out how to sort an array of strings alphabetically. As of now, my code reads string input from the user and displays them in both order and reverse order: Syst ...

The load function within THREE.FileLoader is specifically designed to retrieve the textual content of the index.html file

I've been working on a small web project using the create-js-app tool. I am attempting to load a file named test.txt from my src directory. Here is the code from my main.js file: import * as THREE from 'three'; const loader = new THREE.Fil ...

How can I automatically update the content of a specific Div element when the page is loading using the Ajax load() function

This is the HTML code I have: <body onload="fun1()"> <ul id="nav" class="nav" style="font-size:12px;"> <li><a href="#" id="m_blink" onclick="fun1()">Tab1</a></li> <li><a href="#" id="d_blink" onclick="f ...