0

I want to get the first name of a customer and use that in other functions.

I have an address:

John Doe Streetname 1 1234AA Village

function firstName(){
        let address = "John Doe\nStreetname 1\n 1234AA Village";
        let firstname = adres.split(' ')[0]; 
    }

function customerText(){
   firstName();
   let text = "Dear "+firstname+", how is life?";
   alert(text);
}

So that the result would be: Dear John, how is life?

But unfortunately, it doesn't. Don't know how this should work?

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
Maurice69
  • 45
  • 7
  • 3
    `firstname` is scoped to `firstName()` when you use `let`, so `customerText()` cant' see it. You could use a global scope, or return `firstname` from `firstName()` and then set `firstname = firstName()` in `customerText()`. – mykaf Sep 06 '22 at 14:33
  • 2
    Also `adres.split(' ')[0];` should be `address.split(' ')[0];`. Also also, first names can have spaces. `Mary Beth Doe`, for example; first name is "Mary Beth", not "Mary". Splitting on `' '` is fine for learning code, but for anything legal, this should not be used to determine first/surname, etc. – Tim Lewis Sep 06 '22 at 14:36
  • You need to declare firstname in a scope that is available to customerText(), in your example, the variable firstname, is only available to the function scope of firstname(). Alternatively, and in my opinion more elegantly, you could let firstName() return a string and then assign the result of the function call. – Ross Bush Sep 06 '22 at 14:36
  • Sorry sorry sorry, I see some translations typos indeed (adres -> address) – Maurice69 Sep 06 '22 at 15:13
  • @TimLewis You are totally right about that it is not perfect (missing the second name), but for my purpose it will do at the moment. – Maurice69 Sep 06 '22 at 16:44
  • Yeah, like I said, for testing code and understanding functions like split, etc., there is nothing wrong with your code. I only bring it up as my wife receives so many documents where her name is incomplete due to similar approaches. Cheers! – Tim Lewis Sep 06 '22 at 16:46

2 Answers2

0

return the firstname and use it in the second function

function firstName(){
  let address = "John Doe\nStreetname 1\n 1234AA Village";
  let firstname = address.split(' ')[0]; 
  return firstname;
}

function customerText(){
   let text = "Dear "+firstName()+", how is life?";
   alert(text);
}

// call your function
customerText();

OR pass firstname as parameter to the second function

function firstName(){
  let address = "John Doe\nStreetname 1\n 1234AA Village";
  let firstname = address.split(' ')[0]; 
  customerText(firstname);
}

function customerText(nameParameter){
   let text = "Dear "+nameParameter+", how is life?";
   alert(text);
}

// call your function
firstName();
mmh4all
  • 1,612
  • 4
  • 6
  • 21
  • It seems like the OP's `firstName()` function may be used for multiple things ,not just for the customer text, so it shouldn't automatically call `customerText()`. – mykaf Sep 06 '22 at 14:39
  • FirstName() is the function that needs to be called in more other functions. In the first example you only call customerText() so I don't understand how script also runs firstName()? – Maurice69 Sep 06 '22 at 15:02
-1

Here is the modified code: I hope this will help you.

function firstName() {
    let address = "John Doe\nStreetname 1\n 1234AA Village";
    let firstname = address.split(' ')[0];
    customerText(firstname);
}

function customerText(fname) {
    let text = "Dear " + fname + ", how is life?";
    alert(text);
}

firstName();
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32