skip to Main Content

i have an array that looks like this

let = ["1", "0", "0", "0", ".", "0", "0"]

i want to change the value of last index and then constantly move to the indexes of 0’s

for example i want to input 5,3,2. the output should become

updatedArray = ["1", "0", "0", "5", ".", "3", "2"]

is this possible?

i tried this to check for the indexes of 0

const stringValueArray = stringValue.split('');

const emptyStringIndices: number[] = [];
          stringValueArray.forEach((element, index) => {
            if (element === '0') {
              emptyStringIndices.push(index);
            }
          });
const highestIndex = Math.max(...emptyStringIndices);

       stringValueArray[highestIndex] = value;

const newValue = stringValueArray.join('');
 return newValue;

2

Answers


  1. New answer,

    On second thought, if you reverse the data array, you can map() over it, return input.pop() if there is some (|| c) as fallback, and then reverse it again.

    let data = ["1", "0", "0", "0", ".", "0", "0"],
        input = [ "5", "3", "2" ];
    
    data.reverse();
    
    data = data.map((c, i) => (c === "0") ? (input.pop() || c) : c).reverse();
    
    console.log(data)

    Original answer:

    You can create a loop, in which you hold a flag for the last changed index.

    Then check if that index is equal to "0", if so, replace it with the last item in the changed array, using pop() so we can use input.length as the while condition

    let data = ["1", "0", "0", "0", ".", "0", "0"],
        input = [ "5", "3", "2" ];
    
    
    let lastIndex = data.length - 1;
    while (input.length && lastIndex > 0) {
        if (data[lastIndex] == 0) {
            data[lastIndex] = input.pop();
        }
        lastIndex--;
    }
    
    console.log(data)
    Login or Signup to reply.
  2. Keep track of the original digits and the new digits entered. This is necessary, because you may need to enter 402 to get 1004.02, and you would not want the code to get confused and give 1000.42 as the result because it did not realise that the zero you entered needs to stay.

    It is assumed that the input always contains the decimal place followed by two decimal numbers.

    Then, get the result by combining the original digits with the entered digits, taking care to re-insert the decimal point before returning the result.

    If too many digits are entered, ignore any excess digits.

    class Keypad {
      constructor(s) {
        this.origDigits = [...s].filter(c =>c !=='.');
        this.maxKeyPresses = this.origDigits.filter(c => c==='0').length;
        this.keyPresses = [];
      }
      keyPressed(n) {
        if(this.keyPresses.length < this.maxKeyPresses) this.keyPresses.push(n);
      }
      get val() {
        let digits = [
          ...this.origDigits.slice(0, this.origDigits.length - this.keyPresses.length),
          ...this.keyPresses
        ];
        digits.splice(-2, 0, '.');
        return digits.join('');
      }
    }
    
    let k = new Keypad('1000.00');
    console.log(k.val);
    k.keyPressed(5);
    console.log(k.val);
    k.keyPressed(3);
    console.log(k.val);
    k.keyPressed(2);
    console.log(k.val);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search