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

191
Views
what is wrong to display if condition correctly?

var arr = [{
  name: "John Doe"
}, {
  name: "jamal"
}, {
  name: "Badr"
}, {
  name: "mohsen"
}]

arr.forEach((val) => {
  firstLetter = val.name.split(" ")[0].split("")[0];
});

for (var i = 0; i < arr.length; i++) {
  if (firstLetter == "J" || firstLetter == "j") {
    console.log("Goodbye " + arr[i].name);
  } else {
    console.log("Hello " + arr[i].name);
  };
};

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

0

In your code, the firstLetter has only the last value in it m as you're overwriting its value. Change the code to:

var arr = [{
  name: "John Doe"
}, {
  name: "jamal"
}, {
  name: "Badr"
}, {
  name: "mohsen"
}]

const firstLetter = [];
arr.forEach((val) => {
  firstLetter.push(val.name.split(" ")[0].split("")[0]);
});

for (var i = 0; i < arr.length; i++) {
  if (firstLetter[i] === "J" || firstLetter[i] === "j") {
    console.log("Goodbye " + arr[i].name);
  } else {
    console.log("Hello " + arr[i].name);
  };
};

about 4 years ago · Juan Pablo Isaza Report

0

If I understand correctly, you want to say "Goodbye " + val.name for names which start with J or j (and "Hello " + val.name for others). There's no need to do two loops for this (do the forEach or the for loop).

In any case, you can address the first character in the name as .name[0] and use comparison operators or String methods to determine if the name starts with a J or a j.

See the following code snippet for an example of this.

var arr = [{
  name: "John Doe"
}, {
  name: "jamal"
}, {
  name: "Badr"
}, {
  name: "mohsen"
}];

arr.forEach((val) => {
  if (val.name[0] === "J" || val.name[0] === "j") {
    console.log("Goodbye " + val.name);
  } else {
    console.log("Hello " + val.name);
  }
});

about 4 years ago · Juan Pablo Isaza Report

0

You do not need an other loop after forEach, what you need is to check if the specific name starts with J/j

var arr = [{
            name: "John Doe"
        }, {
            name: "jamal"
        }, {
            name: "Badr"
        }, {
            name: "mohsen"
        }]
    
        arr.forEach((val) => {
            firstLetter = val.name.split(" ")[0].split("")[0];
    
            if (firstLetter == "J" || firstLetter == "j") {
                console.log("Goodbye " + val.name);
            } else {
                console.log("Hello " + val.name);
            };
    
        });
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!