My code isn't working from a challenge in teamtree house
var input1 = prompt("Choose an integer");
var bottomNumber = parseInt(input1);
var input = prompt("choose a second integer");
var topNumber = parseInt(input);
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
var message = "<p>" + randomNumber + " is a number between " + bottomNumber "and " + topNumber + ".</p>";
document.write(message);
I tried to Create a program that has 2 prompts both asking for integers then the it creates a random number betweeen the 2 numbers but on the website the prompts do not appear at all and the javaScript Console has the error:
Uncaught SyntaxError: Unexpected string
Thanks for all the help.
This should work:
var input1 = prompt("Choose an integer");
var bottomNumber = parseInt(input1);
var input = prompt("choose a second integer");
var topNumber = parseInt(input);
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
var message = "<p>" + randomNumber + " is a number between " + bottomNumber + "and " + topNumber + ".</p>";
document.write(message);
Try to interpret the error that appears in the console for a better idea.
The issue was missing a '+' and opening up the browser console will pretty much indicate that.
You are missing a + in your string building.
var message = "<p>" + randomNumber + " is a number between " + bottomNumber + "and " + topNumber + ".</p>";
Fiddle: http://jsfiddle.net/wLpyzwLL/
Related
I am working on creating a small 5 question addition quiz that asks the user a new random addition question every time they answer the previous one. I would like to be able to have my script ask a new question every time the user clicks on the "check answer" button without having to refresh the page. Then, once the user has completed the 5 questions, I would like to be able to have a popup state they have completed the quiz and can refresh the page to start a new quiz. With my current code, the script asks a random addition question, and then once the user clicks on the "check answer" button, the user must refresh the page to get a new question. I am stuck on figuring out how to modify my current code to fit what I am trying to do. I am thinking that a while loop may be they way to go, but I don't really know how to implement it. The code that I have so far is: `
<h1>Addition Quiz!</h1>
<p>This short 5 question quiz will test your addition skills! Answer the question as the computer asks it and pat yourself on the back for each correct answer!</p>
<h3 id="mathquestion"></h3>
<input type="text" name="answerbox" id="answerbox">
<button onclick="addition()">Check Answer</button>
<script>
var minimum = 1;
var maximum = 9;
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
var questionanswer = intiger1 + intiger2;
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
}
</script>`
So what you want is the part that setups your question into a new function. And run that function when the answer is right. An example:
<script>
var minimum = 1;
var maximum = 9;
var questionanswer = 0;
setupQuestion();
function setupQuestion(){
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
questionanswer = intiger1 + intiger2;
}
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
setupQuestion();
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
}
</script>`
Just update the variable
<script>
var minimum = 1;
var maximum = 9;
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
var questionanswer = intiger1 + intiger2;
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
// Updating question
intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
}
</script>
I am writing a code that produces a random math quiz. However, when I run it (in an app called SoloLearn) it I get a Syntax error even though there doesn’t appear to be any. I even tested it in JSHint and no Syntax errors seemed to be present. Here is my code:
function random(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function start() {
var form = document.getElementById("name_form");
var name = form.elements[0].value;
var score = 0;
alert("Let's begin!");
for (let i = 0; i < 4; i++) {
var num1 = random(10);
var num2 = random(10);
var operator = random(2);
if (operator == 0) {
var question = prompt(num1 + " + " + num2 + " =");
var answer = num1 + num2;
}
else {
var question = prompt(num1 + " - " + num2 = " =");
var answer = num1 - num2;
}
if (question == answer) {
alert("Correct!");
score++;
}
else {
alert("Incorrect ):");
}
}
document.write(score);
}
What is going on here?
In your line var question = prompt(num1 + " - " + num2 = " ="); you have misspelled a '+' which is a '=' in your code.
It should be:
var question = prompt(num1 + " - " + num2 + " =");
The same as #Nick Parsons said in the comments
Syntax error should be fixed by changing
var question = prompt(num1 + " - " + num2 = " =");
to
var question = prompt(num1 + " - " + num2 + " =");
which is the intent I believe
https://jsfiddle.net/kelliwilli/6bgrt7bL/128/
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100;
alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent "%!");
}
Seems to just be a simple syntax error, which is not what I thought you were getting at with the question or lack of...
Anyways alert ("There has been " + drawPercent + "% of draws. I won " + AliPercent + "%, and" +p1 +" won" + p1Percent + "%!"); should fix it. You simply forgot to concat the last variable.
Look into template literals, they're super handy for throwing together strings with many variables, along with the fact I find them to be awesome syntax wise.
Template literal form:
alert(`There has been ${drawPercent}% of draws. I won ${AliPercent}%, and ${p1} won ${p1Percent}%!});
https://jsfiddle.net/kelliwilli/6bgrt7bL/224/
//percent funtion
var percent = function() {
var totalGames = (scores.draw + scores.p1 + scores.Ali);
var drawPercent = ((scores.draw / totalGames) * 100);
var AliPercent = ((scores.Ali / totalGames) * 100);
var p1Percent = ((scores.p1 / totalGames) * 100);
alert("There has been " + drawPercent + "% of draws. Ali won " + AliPercent + "%, and " + p1 + " won " + p1Percent + "% of the games! Thanks for playing!!!");
}
There was some code out of sequence, calling a function before it was defined, and an "if" loop that should have been a "while".
Simple fixes. :)
This fiddle link works!
This code only gets me to link address but doesn't run the function. (the last alert() function doesn't run).
I need this alert, what I am doing wrong?
Here is my code:
<html>
<head>
<title>Laboratorul nr_3</title>
<meta charset="utf-8"/>
<script language="javascript"><!--
function ex1()
{
num = prompt("Input the number");
result = num * 3;
alert(num + " x " + 3 + " = " + result);
num2 = prompt("Input number to add");
result2 = num * 3 + parseint(num2);
alert(num + " x " + 3 + " + " num2 " = " + result2);
}
//--></script>
</head>
<body>
link
</body>
</html>
You have to cancel the native action (following the link):
function ex1() {
// code...
return false;
}
Or use a different element that doesn't do anything, maybe a <button> or <div>.
There was only 3 errors you didn't call the function and you didn't concate the string and the parseInt, int cannot be spelled in lower case
function ex1(){
num = prompt("Input the number");
result = num * 3;
alert(num + " x " + 3 + " = " + result);
num2 = prompt("Input number to add");
result2 = num * 3 + parseInt(num2);
alert(num + " x " + 3 + " + " + num2 +" = " + result2);
}
ex1()
I am making a basic game with popup boxes.
My problem is that if you look at the goblinAttack function it will repeat till the goblin is dead but the damage is random.
For example, I hit the goblin 6 damage every time until it dies instead of it being a random number between 1 and 25 and the goblin does 4 damage on me every time it hits. Its boring. I wanted it to be random each hit but repeating the function seems to not give a random number each time.
//VARIABLES
var dmgMultiply = Math.floor(Math.random() * 10 + 1);
var dmg = 10;
var armour = 0;
var hp = 100;
//ENEMY VARIABLES
var dmgGoblin = Math.floor(Math.random() * 10 + 1);
var goblinHP = 100;
//ARRAYS
var choiceArray = ["Type 'sword' equip your sword.",
"Type 'run' if you're scared.",
"Type 'stats' to see your stats."];
var answerArray = ["sword", "run", "stats"];
var outcomeArrayOne = ["You equip your sword.",
"You run away. Game Over."];
var outcomeArrayTwo = ["Sword Equipped."]
//GAME CHOICE
function choice(a, b, c, x, y, z, aa, bb)
{
var answer = prompt(a + "\n" + b + "\n" + c);
if(answer == x)
{
alert(aa);
swordEquipped(outcomeArrayTwo[0]);
}
if(answer == y)
{
alert(bb);
}
if(answer == z)
{
displayStats();
}
}
//EQUIPPED SWORD
function swordEquipped(a)
{
dmg = 25;
dmgMultiply = Math.floor(Math.random() * 25 + 1);
alert(a + "\n" + "DMG : " + dmg);
goblinCombatStart("goblin");
}
//GOBLIN COMBAT START
function goblinCombatStart(g)
{
alert("A wild " + g + " appears!" + "\n" + "The " + g + " has 100 HP!");
goblinAttack("goblin")
}
function goblinAttack(g)
{
alert("The " + g + " swings his axe at you!" + "\n" + "The " + g + " does " + dmgGoblin + " damage to you!");
hp -= dmgGoblin;
var attack = prompt("Type 'attack' to swing your sword at the " + g + "\n" + "HP : " + hp);
if(attack == "attack")
{
alert("You swing your sword at the " + g + "\n" + "You did " + dmgMultiply + " to the " + g);
goblinHP -= dmgMultiply;
alert("The " + g + " is on " + goblinHP + "HP");
if(goblinHP < 0)
{
goblinDead();
}
if(goblinHP > 0)
{
goblinAttack("goblin");
}
}
else
{
alert("You dropped your sword an shouted " + "'" + attack + "'" + " in the goblins face. It wasn't very effective and the goblin choppped your head off");
}
}
function goblinDead()
{
alert("You have slain the puny goblin with only a few scratches!");
}
//GAME START
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
//STATISTICS
function displayStats()
{
var statCheck = confirm("HP : " + hp + "\n" +
"ARMOUR : " + armour + "\n" +
"DMG : " + dmg + "\n" +
"Press OK to continue");
if(statCheck == true)
{
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
}
}
Instead of computing the random damage value only once at the beginning of your script, compute them whenever you need them. That would probably be in your attack function.
Maybe you are misunderstand something here: Math.random doesn't return some special magical value which changes every time it's read. It returns a number and that number doesn't change.
If you want multiple random values, you have to call Math.random multiple times. If you want a different random value whenever the goblin attacks, you have to call Math.random when it attacks.
The problem here is that you're initializing your variables at the beginning as random.
That means that, on every move, it will use the same random number that was generated at the beginning.
If you want it to be random for each time, you have to call Math.random() on every move.
As it stands, dmgMultiply and dmgGoblin are computed randomly once and use the same value on each turn, so you end up getting the same amount of damage taken and dealt each time.
Declare your variables at the beginning:
var dmgMultiply;
var dmgGoblin;
Then, set them to a random number within your attack function so that it computes a random number each turn:
function goblinAttack(g)
{
dmgMultiply = Math.floor(Math.random() * 10 + 1);
dmgGoblin = Math.floor(Math.random() * 10 + 1);
...
}
Demo