Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Can someone please assits me as to why this function is not doing what i want it too

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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"))

about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!