var firstDefrost = "4.0";
function zero(zeroArray) {
if (zeroArray[1] == undefined) {
zeroArray[1] = 0;
zeroArray[2] = "";
console.log("zero array 1 called")
} else if (zeroArray[1] == ".") {
zeroArray[1] = 0;
zeroArray[2] = "";
/*I want it to return 40 not 4.0 which is what is happening */
console.log("zero array 2 called" + zeroArray)
}
return zeroArray;
}
firstDefrost = zero(firstDefrost);
In the second else if block of code I want the value to be returned of 40 but instead it returns 4.0 which I don't want.
From the cases in the OP, it looks like the objective is to take a number represented by a string param, and return a string representing that number times 10.
function zero(string) {
return `${+string*10}`
}
console.log(zero("4.0"))
console.log(zero("3"))
This happens because when you use zeroArray[1] you can get the character in that position of the string but you cannot assign a new value like that, you need an array to do this operation. Let me edit your code with some little changes:
var firstDefrost = "4.0";
function zero ( zeroArray ){
zeroArray = zeroArray.split("");
if(zeroArray[1] == undefined){
zeroArray[1] = "0";
zeroArray[2] = "";
console.log("zero array 1 called")
}
else if(zeroArray[1] == "."){
zeroArray[1] = "0";
zeroArray[2] = "";
/*I want it to return 40 not 4.0 which is what is happening */
console.log("zero array 2 called" + zeroArray)
}
return zeroArray.join("");
}
firstDefrost = zero(firstDefrost);