No prompt from JS - javascript

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()

Related

Can not add up variables NaN

I couldn't have the variables add up as total and neither could I make them multiply inside the var.
What am I doing wrong?
var order;
var amountsoda;
var amountbeer;
var amountwine;
var total = amountsoda + amountbeer + amountwine;
while (order != "stop") {
order = prompt("What order would you like to add? \n\n soda 2 dollar \n beer 5 dollar \n wine 10 dollar")
if (order == "soda") {
amountsoda = prompt("How much " + order + " would you like to add.");
} else if (order == "beer") {
amountbeer = prompt("How much " + order + " would you like to add.");
} else if (order == "wine") {
amountwine = prompt("How much " + order + " would you like to add.");
}
}
document.write("soda: " + amountsoda + " x 2 =" + amountsoda * 2);
document.write("<br>")
document.write("beer: " + amountbeer + " x 5 =" + amountbeer * 5);
document.write("<br>")
document.write("wine: " + amountwine + " x 10 =" + amountwine * 10);
document.write("<br>")
document.write("total: " + total);
You have to initialize the product-amounts and the total with '0'
You need to add up the total inside the while-loop depending on the product chosen
var order;
var amountsoda = 0;
var amountbeer = 0;
var amountwine = 0;
var total = 0
while (order != "stop"){
order = prompt("What order would you like to add? \n\n soda 2 dollar \n beer 5 dollar \n wine 10 dollar")
if (order == "soda" ){
amountsoda = prompt("How much " + order + " would you like to add.");
total = total + amountsoda * 2;
}
else if (order == "beer"){
amountbeer = prompt("How much " + order + " would you like to add.");
total = total + amountbeer * 5;
}
else if (order == "wine"){
amountwine = prompt("How much " + order + " would you like to add.");
total = total + amountwine * 10;
}
}
document.write ("soda: " + amountsoda + " x 2 =" + amountsoda*2);
document.write ("<br>")
document.write ("beer: " + amountbeer + " x 5 =" + amountbeer*5);
document.write ("<br>")
document.write ("wine: " + amountwine + " x 10 =" + amountwine*10);
document.write ("<br>")
document.write ("total: " + total);

“Syntax errors” appearing even though none seem to be present

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

Global variable placement for functions

I'm currently taking a JavaScript class and have the following assignment:
"Design a JavaScript program that asks the user for two numbers, and then sends these two numbers as arguments to four arithmetic functions: addition, multiplication, division, and modulus... The functions should return the values to the calling module, where they're displayed."
<body>
<h1>Number Functions</h1>
<!-- FORM START -->
<form name="Numbers">
First number:
<br/>
<input type="number" name="number1"/>
<br/>
Second Number:
<br/>
<input type="number" name="number2"/>
<p/>
<input type="button" value="Submit" onclick="showInfo()"/>
<p/>
<textarea rows="8" colspan="150" name="info" readonly="true" value=""></textarea>
</form>
<script type="text/javascript">
function showInfo() {
// Declare and get variables
var firstNumber = parseFloat(document.Numbers.number1.value); // first number
var secondNumber = parseFloat(document.Numbers.number2.value); // second number
// Four mathematical functions
function add(num1, num2) {
var sum = num1 + " + " + num2 + " = " + (num1+num2).toFixed(2) + "\n";
return sum;
}
function multiply(num1, num2) {
var product = num1 + " \u00d7 " + num2 + " = " + (num1*num2).toFixed(2) + "\n";
return product;
}
function divide(num1, num2) {
var dividend = num1 + " / " + num2 + " = " + (num1/num2).toFixed(2) + "\n";
return dividend;
}
function modulus(num1, num2) {
var surplus = num1 + " % " + num2 + " = " + (num1%num2).toFixed(2);
return surplus;
}
var total = add(firstNumber,secondNumber) + multiply(firstNumber,secondNumber) + divide(firstNumber,secondNumber) + modulus(firstNumber,secondNumber);
document.Numbers.info.value = total;
// I need this function to get the results from the four above and put them in the textarea!
}
</script>
</body>
I'm not exactly sure how I should be getting the results of these four functions and displaying the results, but I do suspect that a lot of the reasons my code isn't working is because of incorrect variable placement. This is an online class and the textbook is horrible! So if you could point me in the right direction (or multiple correct directions), that'd be great.
There are a lot of things wrong with this.
First, you shouldn't set num1 and num2 at the very top of your script. They will start out not set, so you should retrieve them in your showInfo function.
You aren't returning anything from your functions, which you should be
You aren't actually calling your functions in showInfo, you are coercing them to a string, and thus seeing odd behavior.
It sounds like you should be passing in num1 and num2, not relying on a global variable to access them. You need to pass them and have each function accept two variables.
Try something like this fiddle: https://jsfiddle.net/oLb1vzrb/
// Four mathematical functions
function add(num1, num2) {
return num1 + " + " + num2 + " = " + (num1 + num2).toFixed(2);
}
function multiply(num1, num2) {
return num1 + " \u00d7 " + num2 + " = " + (num1 * num2).toFixed(2);
}
function divide(num1, num2) {
return num1 + " / " + num2 + " = " + (num1 / num2).toFixed(2);
}
function modulus(num1, num2) {
return num1 + " % " + num2 + " = " + (num1 % num2).toFixed(2);
}
function showInfo() {
// Declare and get variables
var num1 = parseFloat(document.Numbers.number1.value); // first number
var num2 = parseFloat(document.Numbers.number2.value); // second number
document.Numbers.info.value = add(num1, num2) + multiply(num1, num2) + divide(num1, num2) + modulus(num1, num2);
}

Array elements not displaying in Javascript

I am new to Javascript. Below is simple program where I am trying to display elements from array that I have created. It does not work. Can anybody please let me know what I am doing wrong?
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Javascript Excercises - Functions</title>
<script>
function findPrimeFactors(){
var primefacts = [];
var primefacs;
var unum = prompt("Please enter a positive number");
var i = 2;
var num = parseInt(unum);
if (num > 0) {
while (num >= i){
if (num % i == 0){
primefacts.push(i);
num = num / i;
console.log("Prime factor: " + i + " New num: " + num + " Array length: " + primefacts.length + " Last array element: " + primefacts[primefacts.length-1]);
}
else {
i += 1;
}
};
if (primefacts.length = 0) {
document.write("No prime factors other than 1 for this number.");
}
else {
primefacs = primefacts.join();
console.log("Prime factors: " + primefacts[0] + ", " + primefacts[1] + ", " + primefacts[2]);
document.write("The prime factor for " + unum + " are : " + primefacs);
}
}
}
</script>
</head>
<body>
<button onclick="findPrimeFactors()">Click to proceed</button>
</body>
replace:-
if (primefacts.length = 0) {
with
if (primefacts.length == 0) {
you are setting the length to 0 instead of comparing.
function findPrimeFactors() {
var primefacts = [];
var primefacs;
var unum = prompt("Please enter a positive number");
var i = 2;
var num = parseInt(unum);
if (num > 0) {
while (num >= i) {
if (num % i == 0) {
primefacts.push(i);
num = num / i;
console.log("Prime factor: " + i + " New num: " + num + " Array length: " + primefacts.length + " Last array element: " + primefacts[primefacts.length - 1]);
} else {
i += 1;
}
};
if (primefacts.length == 0) {
document.write("No prime factors other than 1 for this number.");
} else {
primefacs = primefacts.join();
console.log("Prime factors: " + primefacts[0] + ", " + primefacts[1] + ", " + primefacts[2]);
document.write("The prime factor for " + unum + " are : " + primefacs);
}
}
}
<button onclick="findPrimeFactors()">Click to proceed</button>

Why isn't my code working? (TeamTreeHouse)

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/

Categories