The problem is a 3 part problem which required me to make a function called addOne that took a number as it's argument and added one to it then create a function called subtractOne that took a number argument and subtracted one from it. After that I had to create a function called usePotions that took a string as it's argument and if the string had the character "A" in it, you would need to use the addOne function on the digit before "A" if it was available. If the string had the character "B" in it, you would need to use the subtractOne function on the digit before the "B" if it was available. You also want to remove the the letters from the string and just return the string with the math performed on the numbers if it was available. This is what I have so far:
function addOne (num) {
num += 1;
return num
}
function subtractOne(num1) {
num1 -= 1;
return num
}
function usePotions(string) {
let newArray = [];
for (let i = 0; i < string.length; i++) {
if (typeof string[i] === "A" && typeof string[i-1] === "number") {
newArray.push(addOne(string[i-1]));
} else if (typeof string[i] === "B" && typeof string[i-1] === "number") {
newArray.push(subtractOne(string[i-1]));
} else if (typeof string[i] === "number"){
newArray.push(string[i]);
}
}
return newArray.join('');
}
console.log(usePotions("3A78B51"))
*When I tried checking to see if the array was getting values passed into it the array remained empty and I'm not sure why. Thank you for your help!
Aucun commentaire:
Enregistrer un commentaire