I'm working with strings that look like this:
"do3mi3so3 la3 ti4 do5 re5 mi5 /2 x2 fa4"
I'd like to increment each number in the string past a specific point exempting numbers next to '/' or 'x'. For example, if I want to increment all the numbers of the aforementioned string past the point do3mi3so3, I'd expect to get this:
"do3mi3so3 la4 ti5 do6 re6 mi6 /2 x2 fa5"
Here is my code:
function is_numeric(str){
return /^\d+$/.test(str);
}
function change_octave(notes,split_point){
for(var i=(split_point-1);i<notes.length;i++){
if(is_numeric(notes[i])==true){
notes[i]='' + parseInt(notes[i])+1;
//console.log(parseInt(notes[i])+1) //these numbers are incrementing
}
if(notes[i]=='/'||notes[i]=='x'){
i = i+3;
}
}
return notes;
}
var notes = "do3mi3so3 la3 ti4 do5 re5 mi5 /2 x2 fa4";
console.log(change_octave(notes,4));
Despite the numbers successfully incrementing, the value of the string does not change.
Thanks.
You can't actually set the character of a string at a specific index using something like notes[i] = "a"
. What you can do is split the string into an array, change the values in that array, and then join it back together into a string when you're done.
function is_numeric(str){
return /^\d+$/.test(str);
}
function change_octave(notesStr,split_point){
const notes = notesStr.split('');
for(var i=(split_point-1);i<notes.length;i++){
if(is_numeric(notes[i])==true){
const newValue = parseInt(notes[i]) + 1;
notes[i] = '' + newValue;
}
if(notes[i]=='/'||notes[i]=='x'){
i = i+3;
}
}
return notes.join('');
}
var notes = "do3mi3so3 la3 ti4 do5 re5 mi5 /2 x2 fa4";
console.log(change_octave(notes,4));
In JavaScript, a string is a primitive type and is therefore immutable. By default, in so-called "sloppy mode", assigning a value to an element of a string silently fails, but in strict mode it will throw an error:
"use strict";
let notes = "do3mi3so3 la3 ti4 do5 re5 mi5 /2 x2 fa4";
notes[4] = "o";
In order to accomplish this, I would suggest making use of String.prototype.replace()
like this:
function change_octave(notes, split_point) {
return notes.replace(/\d+/g, (str, i) => {
if (--split_point > 0) {
return str;
}
if (i > 0 && (notes[i - 1] === '/' || notes[i - 1] === 'x')) {
return str;
}
return Number(str) + 1;
});
}
let notes = "do3mi3so3 la3 ti4 do5 re5 mi5 /2 x2 fa4";
console.log(change_octave(notes, 4));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With