Please help me how to change the letter in the array without creating a new array ?
Can you show us what code you have tried so far?
array[0].charAt(0) = “9”;
What does your full code look like?
What does arr
look like?
If array[0]
is a string, you can not mutate it. Strings are not mutable. You can create a new string and assign it to array[0]
but you can not directly change a character of a string.
let array = [‘109’, ‘102’, ‘100’, ‘030’, ‘020’, ‘006’, ‘001’];
You have to fully replace array[0]
since it is a string. You can’t just change part of it.
Side note - the more code you show, the easier it is for us to understand what’s going on.
For example, I need 101 to get 901.
Let’s say I have the following code:
let aString = `some string';
I can not do this:
aString.charAt(0) = 'S'
but I could do:
let newStr = 'S' + aString.slice(1);
aString = 'S' + aString.slice(1);
console.log(aString); // displays "Some string"
Let’s just say that such a function should be replaced by 9 so that there is a maximum difference in the sum
let array = ['109', '102', '100', '030', '020', '006', '001'];
let k = 5;
let swith = 0;
function replacement(k, array) {
for (let i = 0; i < k; i++) {
for (let j = 0; j < array.length; j++) {
let firstLetter = array[j].charAt(swith);
if (firstLetter != "0" && firstLetter != "9") {
let currentValue = array[j];
if (minValue == null || currentValue < minValue) {
minValue = currentValue;
};
} else {
break;
};
};
};
};
What should be replaced?
Maximum difference in what sum?
Maybe if you give us the full problem description, we can better help you. I am not understanding what the function is supposed to do and what swith
or k
is supposed to represent.