Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

ziaul sarker
ziaul sarker
3,652 Points

search name in text and add it to array

var name = prompt("what is youre name");
var adress = prompt("what is youre leagal adress");
var value = prompt("what value would you hold if you are hired by the company");
var wages = prompt("what would you like to get paid");
var goals = prompt("what are youre goals");
var newArray = []


var text = "Heloo my name is " + name + ". i live on " + adress + ". i would hold a lot of value to youre company " + value + ". i would like to get paid $" + parseInt(wages) + ". my ultomate goal is to " + goals


for (i=0; i < (text.length); i++) {
    if (text[i] === (name.substring(0,1))) {
        for (n=i; n < (name.length); n++) {
            newArray.push(text[n])
        }
    }
}

if (newArray === 0) {

    console.log("you did not enter your name");

   for (i=0; i < (text.length); i++) {
    if (text[i] === (name.substring(0,1))) 

        for (n=i; n < (name.length); n++) {

                newArray.push([n])

            }
    } 

}


else {
   console.log(newArray)
}



/* when i run the code the array is still left blank nd the loop does not start over again......i get this message from the console [] undefined */
Andrew Breslin
Andrew Breslin
10,177 Points

I don't know if this is what is wrong, but:

var newArray = []

has no semicolon after the code line

1 Answer

Rich Donnellan
MOD
Rich Donnellan
Treehouse Moderator 27,684 Points

Ziaul,

Your code has quite a few syntax errors which prevents your code from successfully executing. Rather than explain them all, I'll present you with an alternate solution, which you can study. I had to make certain assumptions, but I think I'm still on the mark.

Let me know what you think.

var name = prompt("What is your name?"); 
var address = prompt("What is your legal address?"); 
var value = prompt("What value would you hold if you are hired by the company?");
var wages = prompt("What would you like to get paid?");
var goals = prompt("What are your goals?"); 
var nameArray = [];
var text = "Hello, my name is " + name + ". I live on " + address + ". I would hold a lot of value to your company " + value + ". I would like to get paid $" + parseInt(wages) + ". My ultimate goal is to " + goals;

if (text.search(name)) {
  nameArray.push(name);
  console.log(nameArray);
} else {
  console.log("You did not enter your name.");
}

console.log(text);

-Rich