I’m just starting to learn javascript but it just drives me crazy! This is a question to enter a boot camp for learning javascript because I’ trying to change my career.
Here I have the variable target that is a number up to 10 and I need to write a program that will keep on guessing numbers and printing them until it is the target number. I have five tries, but if I guess it first, it should print a message and break out of the loop, but I simply don’t know what is wrong with it, it keeps giving me some error
var target = Math.ceil(Math.random() * 10);
var guess = Math.ceil(Math.random() * 10);
var tries=5;
console.log ('Target is '+target);
console.log('You have '+tries+' tries');
if (guess==target){
console.log('Congratulations! The number ' + guess +' is right!');
}
else{
while (tries>0){
guess = Math.ceil(Math.random() * 10);
console.log('Number '+guess +' is wrong');
tries --;
console.log('You still have ' +tries + ' tries');
if (guess==target){
console.log('Congratulations! The number ' + guess +' is right!');
break;
}
if (tries==0){
console.log('You had your chance');
}
}
}