-1

I have an assessment for Java script basics that I have not been able to study for due to unforeseen circumstances so I am now having to catch up in the little time I have.

My assessment states that I must find the substring of the start of each string, which I have managed to do but I am having trouble joining those two processed substrings into the one alert box ( I know these are frowned upon but the assessment states I must use this). I have tried using the + operator but this gives me the error of the second variable being produced twice. I have posted my code below for anyone to have a look.

function userName(){
   var name = "Joe";
   var surName = "Bloggs";
   name = name.substring(0,1);
   surName = surName.substring(0,1);
   alert(name);
}
ozil
  • 6,930
  • 9
  • 33
  • 56
JWoods
  • 31
  • 5
  • Why have I been down voted for asking this? I am trying to learn and asking a simple question? – JWoods May 12 '15 at 13:45
  • Welcome to [so]. Did you try performing some research before asking your question? There are numerous places which explain the basics of JavaScript: ["joining two strings javascript"](http://google.com/search?hl=en&q=joining+two+strings+javascript). – Qantas 94 Heavy May 12 '15 at 13:48
  • 1
    I didn't downvote, but I'm guessing it's because you said you've tried `+`, but didn't really give enough details about what _exactly_ you tried and _precisely_ why it didn't work, given that that is (probably) the right way to solve this. – James Thorpe May 12 '15 at 13:48
  • You say you tried the `+` operator, but you don't show what you tried. I didn't downvote you, but I would guess that this and the fact that this is an extremely basic question are the reasons you got downvoted. – elixenide May 12 '15 at 13:49
  • As I said, I have very little time to research at the moment so trying to get it all done quickly as to not waste a years work because of one assessment. But thanks for the reply. – JWoods May 12 '15 at 13:49

3 Answers3

2

Basically you can concat two variables (strings) using the + operator. The type of htese variables are defined dynamically and the + operator will concat them into a single variable (string), after that you can show the result into a alert.

function userName()
{
   var name = "Joe"
   var surName = "Bloggs"
   name = name.substring(0,1)
   surName = surName.substring(0,1)
   var result = name + surName;
   alert(result);
}

On the other hand, to get only the first char of a string variable in javascript, you can treats it as a array of chars and access the first index, (starting in 0) for sample:

function userName()
{
   var name = "Joe"
   var surName = "Bloggs"
   var result = name[0] + surName[0]; // get only the first char
   alert(result);
}

Alternativelly, there a method called string.concat(string) which allows you to concat two strings.

String concatenation This is a very discussed question in many languages and I recommend you to read this thread on stack overflow:

Best way to concatenate strings in JavaScript?

Community
  • 1
  • 1
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • This is correct. It would be even better with some semicolons and one `var` statement. I realize your code tracks OP's code, but might as well teach good code hygiene while we're at it... :) – elixenide May 12 '15 at 13:47
2
function userName(){
   var name = "Joe";
   var surName = "Bloggs";
   name = name.substring(0,1);
   surName = surName.substring(0,1);
   alert(name + surName );
}
ozil
  • 6,930
  • 9
  • 33
  • 56
  • When I tried this out I was shown the surName var twice but now it seems to work, the tutor told us that using semi-colons was old and unneeded but with the addition of them it has worked. Thanks ozil. – JWoods May 12 '15 at 13:48
  • @ JWoods using `semi colons` at the end of the statement is good practice as you have already seen disadvantage of not using semi colon. – ozil May 12 '15 at 13:54
0

Have you tried

alert(name + ' ' + surname)

This is just joining to strings with the a space ' '

Nico
  • 12,493
  • 5
  • 42
  • 62