Using Array.splice() reveals a plethora of information

I'm facing an issue with the splice function. Here is my code snippet:

const findMissingLetter=(array)=>{
  let result, alphabet = "abcdefghijklmnopqrstuvwxyz";
  if(array[0]===array[0].toUpperCase()) alphabet = alphabet.toUpperCase();
  let start = alphabet.split('').indexOf(array[0]);
  return alphabet.split('').splice(start,start+array.length+1);
}

The purpose of this function is to identify a missing letter in the alphabet sequence and return it.

The argument provided will consist of either lowercase or uppercase letters only. The problem arises when I apply this code to the following arrays:

['a','b','c','d','f'] - it works correctly, returning ['a', 'b', 'c', 'd', 'e', 'f']

However, if uppercase letters are used: ['O','Q','R','S'] - the output is

['O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
.

const findMissingLetter = (array) => {
  let result, alphabet = "abcdefghijklmnopqrstuvwxyz";
  if (array[0] === array[0].toUpperCase()) alphabet = alphabet.toUpperCase();
  let start = alphabet.split('').indexOf(array[0]);
  return alphabet.split('').splice(start, start + array.length + 1);
}

console.log(findMissingLetter(['a','b','c','d','f']));
console.log(findMissingLetter(['O','Q','R','S']));

What could be causing this issue?

Answer №1

To get the desired length of the result array, simply use array.length + 1 without considering the start index.

const findMissingCharacter = array => {
    let alphabet = Array.from("abcdefghijklmnopqrstuvwxyz");

    if (array[0] === array[0].toUpperCase()) alphabet = alphabet.map(c => c.toUpperCase());

    let start = alphabet.indexOf(array[0]);

    return alphabet.splice(start, array.length + 1);
}

console.log(...findMissingCharacter(['a', 'b', 'c', 'd', 'f'])); // ['a', 'b', 'c', 'd', 'e', 'f']
console.log(...findMissingCharacter(['O', 'Q', 'R', 'S'])); // ['O', 'P', 'Q', 'R', 'S']

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

Why is the model so tiny after converting my FBX file to a .gltf format?

QUERY: I am facing an issue where my model appears extremely small after converting the FBX file to a .gltf format. Despite attempting to scale the model using frontObject.scale.set(1000, 1000, 1000);, I encounter the following error: TypeError: Cannot r ...

Trying to assign a value to a property that is not defined

I'm attempting to initiate the loading and exhibition of a .stl file through three.js by implementing the following code: var stlLoader = new THREE.STLLoader(); stlLoader.load('assets/Cap.stl', function (object){ object.position.y = - 1 ...

What is the process to obtain the download URL for an image stored in Firebase Storage?

I have encountered an issue while trying to upload an image to Firebase storage and then retrieve the download URL. Here is the code snippet that I am using: const response = await fetch(selectedImage.uri); const file = await response.blob(); const storag ...

Once an action is dispatched, React is unable to access the latest Redux state

Currently, I am in the process of familiarizing myself with React and Redux by working on a personal project. One issue I am facing is that the `isAuthed` variable is unable to access the updated Redux state after the `rest.dispatch(actions.isValidUser(js ...

The promise node design pattern

I'm dealing with a node issue where I need to call a Data Access Object and possibly others within it, and then render a Jade template once everything is done. Here's an example of what I want to achieve: provider1.getData(args, function(error ...

Getting field values from Firestore in a React application

Within my firestore document, I have three fields that store boolean values critical for subsequent processing. To access these boolean values in my program, I need to figure out how to read the fields of a document. This process should be straightforward, ...

What is the best way to incorporate jQuery's default handsontable into an AngularJS directive?

I currently have a handsontable functioning in a non-AngularJS application, and I am in the process of developing a new version of the software that heavily utilizes AngularJS (SPA). My question is: is it possible to encapsulate the existing handsontable ...

JavaScript is unable to activate the button once it has been disabled

JS I am facing an issue with a form that has save and download buttons. I want the download button to be disabled initially, and then enabled once the form is saved. $("#download_form").attr('disabled', 'disabled'); $('.save ...

Saving information in node.js

My latest project involves creating an address book app using HTML, CSS, and JavaScript. The company provided me with a zip file containing the necessary resources to implement the app using node.js. However, my knowledge of node.js is limited and I have ...

Enhancing the visual appeal of a standard jQuery slider with thumbnails

Recently, I incorporated the Basic jQuery slider into my website, which can be found at . As a novice in jQuery but well-versed in HTML and CSS, I have managed to make it work seamlessly on my site. However, I am curious to know if there is a way to displa ...

Toggle visibility of cards using bootstrap

I've been attempting to show and hide a selection of cards in bootstrap, but I'm having trouble figuring it out. All the cards share the class "card," and my goal is to hide them all when a specific button is clicked. Below is my current code: ...

Step-by-step guide on how to showcase elements within v-for by clicking on them

In my data array, only the first element is visible by default. Clicking on either the YES or NO button will display the element with the corresponding id of yes_section or no_section (depending on which button was clicked). For instance, if we click on ...

Having difficulty operating the development environment

Currently enrolled in a Udemy course and facing an issue with running a script. Attempting to execute the command npm run dev results in the following error message: npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! <a href="/cdn-cgi/l/email-protectio ...

Extract information from a JavaScript function utilizing Python's Selenium

Is there a way to extract data from within a JavaScript function using Selenium? Visit the page here Here is the input code: <script type="text/javascript"> var chartData1 = []; var chartData2 = []; var chartData3 = []; ... ...

Use JavaScript to jumble up each individual letter

Is there a way to scramble words letter by letter instead of the whole word at once in Vue.js? I'm new to Vue.js and struggling to make this change in my code. I'm currently using Vue.js for this task. const sampleText1 = 'インバウント ...

Establish a connection with the hivemq broker using MQTT protocol

Within my app.js file: const mqtt = require('mqtt') const client = mqtt.connect('mqtt://localhost:1883') topic = 'testTopic' client.on('connect', ()=> { client.subscribe(topic) }) client.on(&a ...

The class .is-invalid transforms into .is-valid when rendered

Currently, I am incorporating bootstrap into my react project. In this case, I have a variable called mobile that needs to undergo validation whenever there is a change in the input field. Below is the code snippet for the component: const EnterMobile = ( ...

One class seems to be causing issues with hover functionality while the other works perfectly fine

HTML: <div class="channellist"></div> Through the use of Ajax, I am able to dynamically retrieve channels when the page loads and then append them within the channellist container. Once appended, my HTML structure appears as follows: <div ...

Testing the onClick event in React components using unit testing

I'm facing an issue with testing a Button wrapper component that utilizes a material-ui button. I tried writing some test code, but it's failing when trying to test the onClick event. index.tsx (ButtonWrapper Component) import React from &ap ...

Guide on showing JSON data from a specified URL through an AJAX request

Just starting out with JSON and jQuery, I'm diving into the basics by working on some examples. Using existing JSON data from http://api.androidhive.info/contacts for my practice, I want to showcase this data on my HTML page. Below is a snippet of my ...