Eliminate the final two digits from each element in the array if they exceed two digits

A challenge I am facing involves an array with multiple elements. My goal is to remove the last two digits from any element in the array that consists of 3 or 4 digits, while leaving elements with 1 or 2 digits unchanged.

var test =[ 2, 45,567, 3, 6754];

The desired output is :

[ 2, 45, 5, 3, 67];

I attempted a solution using:

test.forEach(function(element, index) {    test[index] = element.slice(1, -2); }); 

However, this approach resulted in blanking out single-digit elements.

Answer №1

To get the integer value, you can divide the number by 100 and then round down if needed.

const
    values = [2, 45, 567, 3, 6754],
    updatedValues = data.map(val => val >= 100 ? Math.floor(val / 100) : val);

console.log(updatedValues);

Answer №2

The solution you provided is ineffective because it attempts to slice values of type number.

Consider using the following code snippet instead:

test.forEach(function(element, index) {    
    test[index] = String(element).length > 2 ? +String(element).slice(0, -2) : test[index]
}); 

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

Incorporating fresh data into an existing array using jQuery

In the database, I have an array created using stored data: var chdata = [ {title: "title one",link: "link one",thumb: "image url one",pcaid: "0001"}, {title: "title two",link: "link two",thumb: "image url two",pcaid: "0002"}, {title: "title three", ...

Meteor is not recognizing the defaultValue property in React

I'm puzzled by this issue—it's frustrating that it's not working as expected. <input type="text" id="first_name" name="first_name" className="form-control" defaultValue={this.props.user.first_name} required/> However, I've di ...

Is it possible to send an AJAX request to a Django view that might result in a redirect?

Query I encountered an issue while attempting to access a specific Django view through AJAX. This particular view redirects users if they haven't authorized the site with Google. I suspect the problem arises from redirecting "within" a view requested ...

Is there a way to confirm that the content of two files is identical?

Currently, I am utilizing mocha/supertest/should.js for testing a REST Service. When I make a request to GET /files/<hash>, it returns the file as a stream. I am seeking guidance on how to use should.js to assert that the contents of the file are i ...

Polymorph 1.0 - Using CSS Class Binding for Dynamic Properties

I attempted to link a CSS Class to a paper-progress element using the value of my property to change the item's color. I referenced Polymer's example on GitHub and studied the documentation on Data-binding. Here is my code: http://jsbin.com/bide ...

Obtain the name of the checkbox that has been selected

I'm new to JavaScript and HTML, so my question might seem silly to you, but I'm stuck on it. I'm trying to get the name of the selected checkbox. Here's the code I've been working with: <br> <% for(var i = 0; i < ...

Modify the JavaScript regular expression to be compatible with Java programming language

I stumbled upon this JavaScript regex that extracts IDs from the Youtube URLs provided below: /(youtu(?:\.be|be\.com)\/(?:.*v(?:\/|=)|(?:.*\/)?)([\w'-]+))/i Youtube URLs tested on: http://www.youtube.com/user/Scobleize ...

Obtain a JSON array in Java Android by retrieving data from another JSON array

I have a JSON array and I am trying to extract the data from it, specifically all subjects. Can someone help me with how to achieve this? Here is the code I have so far: JSONObject jsonObject = new JSONObject(thatarray); JSONArray jsonArray = jsonObject. ...

Ways to receive a POST request from an external server on a GraphQL Server

Currently, I am working on a project that utilizes GraphQL. As part of the project, I need to integrate a payment processor. When a user makes a successful payment, the payment processor sends a POST request to a webhook URL that should point to my server. ...

How to visually deactivate a flat button ( <input type="button"> ) programmatically with JavaScript

I am facing an issue with my buttons. I have one regular button and another flat button created using input elements. After every click, I want to disable the buttons for 5 seconds. The disable function is working properly for the normal button, but for th ...

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

What are some ways to enhance the functionality of the initComplete feature in Dat

$('#example').dataTable( { "initComplete": function(settings, json) { alert( 'DataTables has finished its initialisation.' ); } } ); Is there a way to extend the initComplete function for other languages? $.extend( true, $.f ...

Why is it that when implementing React JS, the function setState is not updating the values on the chart when a Button is

Currently, my webpage is built using React JS and includes a JavaScript chart. I am trying to make the data in the chart dynamically change based on a value entered into a text box. When a button is clicked, the graph should update with the new results. Ho ...

Error encountered: iPad3 running on iOS7 has exceeded the localStorage quota, leading to a

Experiencing a puzzling issue that even Google can't seem to solve. I keep getting the QuotaExceededError: DOM Exception 22 on my iPad3 running iOS7.0.4 with Safari 9537.53 (version 7, WebKit 537.51.1). Despite turning off private browsing and trying ...

Vue 3's defineExpose feature does not allow for the exposure of child methods or properties

I have a main component and subcomponent set up as shown below: Main Component : <script setup> import SubComp from '@/components/SubComp.vue' import { ref, computed } from 'vue' const subComp = ref(null) const handleClick = () ...

implementing a JSON array of integers within an Angular application

I am trying to import a JSON array of integers into scope.data in order to use it for creating D3 bars. However, I am facing issues as the array is not being utilized properly by D3 bars even though it is displayed when using {{data}} in the HTML. Can so ...

Using JavaScript in PHP files to create a box shadow effect while scrolling may not produce the desired result

Issue at hand : My JavaScript is not functioning properly in my .php files CSS not applying while scrolling *CSS Files are named "var.css" #kepala { padding: 10px; top: 0px; left: 0px; right: 0px; position: fixed; background - c ...

How can I apply COUNTIFS with multiple criteria across ranges of varying sizes?

In my attendance tracking system, I am trying to monitor the presence of each type of employee on a daily basis. I have a summary page (sheet1) where I want to calculate the count of each employee type (A,B,C,D,E) for a specific day mentioned in cell C2, ...

Easy methods to navigate between screens without using React Router libraries

After experimenting with two different methods to switch between screens in a simple application (up to 3 modes/screens), I am still in the learning phase and mainly focusing on practicing with useState and possibly useEffect for certain scenarios. I&apos ...

What is the best way to add an array to my JSON object in Javascript?

I'm currently in the process of formatting an array into a JSON object for API submission. Struggling to find the right method to transform my array into the desired structure. This is what my array looks like: data: [ ["Lisa", "Heinz", "1993-04 ...