New course! Every coder should learn Generative AI!
Try a free lesson0
Why is my code not running?
I have started this code, and it looks like this: var age = 1; while(age < 7) age =+ 1; document.write(age) But the problem is, it won't show the var "age" when i run the code. It's probably just me missing something totally obvious but i cant figure it out. Anyone?
7 Answers
+ 3
Write += instead of =+.
+ 1
There isn't any error message because there isn't any error as far as the interpreter is concerned: your "age=+1;" is being interpreted as "age = +1;" (with the plus as an unary operator. In other words, you were just setting your variable to 1 over and over in your loop.
+ 1
It doesn't display any error message because you haven't made it to.
In JS you can use:
Try {
alert("Try runs some code and if an error is found the catch does something.");
}
then use
catch {
alert("Catch does something if an error is found.");
}
You can also use
finally
{
alert("This code will always do something even if no error is found.");
}
Explanation:
Try runs the code.
Catch does something if an error is found.
Finally always runs.
For example:
Try {
aler("Note that the t in alert is missing.");
}
Catch {
alert("Error!");
}
Result:
Error!.
I have no idea why SoloLearn didn't include error handling in their courses.
0
And it also doesnt display any error message
0
the biggest problem is the format:
while (age < 7){
age += 1;
document.write (age);
}
JS does understand that age is in the while loop
0
var age = 1;
while(age < 7)
age =age+ 1;
document.write(age)
agora esta corrigido mas tem que adicionar um vento pra funçao funcionar tipo assim:
var age = 1;
function clicked(){
if(age < 7)
age =age+ 1;
}
document.write("<button onclick='clicked()'>click here</button>"+age)
0
void bot(int mode,string name) {
if(mode==1){
cout<<"Welcome,"<<name<<"!";
}
else if(mode==2){
cout<<"Goodbye,"<<name<<"!";
}
else{
cout<<"Try again";
}
}
Why this is not running it's all correct