Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript simple a+b does not work

Helenneleh

New Coder
Hi,

What am i doing wrong that it does not give the answer"hello world". Instead i get error message: "e is not defined"

JavaScript:
function greeting(k){
var ans=m+e;
m=hello;
e=world;
return ans;
}
var i=greeting(k);

print(i);
 
Im not a Java expert BUT

Code:
m=hello;
e=world;
var ans=m+e;

Should be the other way around, like above. Your setting m as Hello and e as World but after you are trying to set var ans as m and e so technically m & e are empty at that point.
 
simong1993 is correct in the flow of the code, but we also need to make the words variables and have a space to separate them. Print is a bad choice to show the results. Use alert() or console.log() for a modern solution.
Code:
<script>
function greeting(){
    var m='hello ';
    var e='world';
    var ans=m+e;
    return ans;
}
var i=greeting();
alert(i);
</script>
OR a more efficient code
Code:
<script>
function greeting(){
    var m='hello ';
    var e='world';
    return m+e;
}
alert(greeting());
</script
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom