How to achieve this Pricing Calculator [duplicate] - javascript

This question already has answers here:
Price Calculator based on Quantity [closed]
(4 answers)
Closed 8 years ago.
I am trying to create a simple script to add to a html website.
I need it to calculate the price based on the quantity the user inputs.
For example, a value of 1-1000 will be multiplied by 1.50 and displayed, 1001-5000 multiplied by 1.20 and displayed, 5001-10000 multiplied by 1 and displayed and any number above that would display an error message like "Must be below 10000".
The result is to display in a text field so the user can click submit.
I've been trying to do this in js with no success. If this can be done in any other language please let me know. I'm still learning.

function calc(val) {
if (val < 1 || val > 10000) {
alert("Value must be a positive number under 10,000")
return 0;
}
if (val < 1001) return val*1.5;
if (val < 5001) return val*1.2;
return val;
}

This can be done trough almost every language available for web. Calculating is the most easy thing. For example, in PHP:
$output = $input - 5; // -5
$output = $input + 5; // +5
$output = $input++; // +1
$output = $input * 5; // x5
Javascript plus example:
var input = 5;
var plus = 6;
var output = input+plus;
Javascript min example:
var input = 5;
var plus = 6;
var output = input-plus;

Related

Why doesn't my JavaScript program to find odd numbers work? [duplicate]

This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Sum of two numbers with prompt
(10 answers)
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 1 year ago.
I made a simple js code to input start value and end value form prompt and then find all the odd numbers, unfortunately it's not working properly. when i input 1 and 10 it'll work, but when i input 5 for sValue(start value) the program won't work. any idea?
var odd = [];
var sValue = prompt("start");
var eValue = prompt("end");
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);
Because the value of prompt is a string. You need to convert it to a number with parseInt(v, 10).
var odd = [];
var sValue = parseInt(prompt("start"), 10);
var eValue = parseInt(prompt("end"), 10);
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);

Adding prompts to an array in javascript [duplicate]

This question already has answers here:
How to save prompt input into array
(5 answers)
Closed 7 months ago.
I'm busy with a task that requires me to ask the user to keep entering random numbers until the number is "-1". After that I would have to get the average of all the numbers entered excluding the "-1". I've gotten this far with it:
var userNumbers;
while (userNumbers !== "-1") {
userNumbers = prompt("Enter a number");
}
numbersArray = [userNumbers];
console.log(numbersArray);
Try this
// Store all numbers
const numbers = [];
let userNumber;
for(;;){
userNumber = prompt("Enter a number");
if(userNumber === '-1') { break; }
numbers.push(userNumber);
}
// Calculate average
let sum = 0;
let avg = 0;
numbers.forEach((value) => sum += value);
avg = sum / numbers.length

Why isn't this HTML/Javascript code working? [duplicate]

This question already has answers here:
JavaScript: How to reverse a number?
(19 answers)
Closed 3 years ago.
I want to create a program which helps me find the reverse of a number. So, if I enter the number 135, it gives back 531 to me. I created the code, with the help of various online sources that confirm that my method is correct. However, I cannot seem to create a solution. I tried using a while loop in a similar fashion, as well. The output always comes out as Infinity. Is there a problem with my technique or the code.
<input type="button" value="Find the Reverse of a Number" onclick="inv()">
<script>
function inv() {
var n = prompt("Enter a number: ");
var rev = 0;
for (; input !== 0;) {
var lastDigit = input % 10
rev = rev * 10;
rev = rev + lastDigit;
input = input / 10;
}
}
</script>
check it .
var a = prompt("Enter a value");
var b, sum = 0;
var z = a;
while(a > 0)
{
b = a % 10;
sum = sum * 10 + b;
a = parseInt(a / 10);
}
alert(sum);
Try this:
function inv(){
var n = prompt("Enter a number: ").toString();
n = [...n].reverse().join("");
alert(`Reversed ${n}`)
}
<input type="button" value="Find the Reverse of a Number" onclick="inv()">

How to create jQuery thousands separator while keeping .toFixed(2) [duplicate]

This question already has answers here:
Adding space between numbers
(3 answers)
Closed 4 years ago.
I'm working on a price calculation table on Wordpress using jQuery.
I want to space my numbers when the result are in thousands.
Converting this: 1000 into this: 1 000
I tried several jQuery scripts but they all got rid of my .toFixed(2)
var min = 0;
var max = 500000;
jQuery( "#input" ).keyup(function() {
if(jQuery("#input").val() < max && jQuery("#input").val() >= min && jQuery("#input").val() < 1000 ) {
var val = jQuery("#input").val() * jQuery(".price1").val();
jQuery('#amount1').html(val.toFixed(2));
}
});
At the moment the total price is displayed like this:
total price: 2349.30$
I'm trying to convert it like this:
total price: 2 349.30$
You don't need Jquery you can solve it using regular expressions...
https://stackoverflow.com/a/32889998/2894798
var min = 0;
var max = 500000;
var a = 23423;
var b = 309248023;
console.log(max.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))
console.log(a.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))
console.log(b.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))

while loop not working in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having a bit of trouble getting this code to work. I think it has something to do with the while loop. Also I'm new to JavaScript so, if you any suggestions on ways to improve the syntax or make my code more efficient it would be greatly appreciated.
how the code should work.
The user enters a cost of an item and then the amount of
money given. The program will figure out the change and the number of quarters, dimes, nickels, pennies needed for the change.
what happens when run
the two prompt boxes pop up and than I get a alert warning..
"A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue."
https://jsfiddle.net/krighty78/g44ejnbw/1/
/* The user enters a cost of an item and then the amount of
money given. The program will figure out the change and the number of quarters, dimes, nickels, pennies needed for the change.*/
var cost = prompt('please enter total cost of the item without tax');
cost = parseFloat(cost);
var moneyGiven = prompt('please enter the amount of money given');
moneyGiven = parseFloat(moneyGiven);
var tax = 0.15;
tax = (cost * tax);
var quarter = 0;
var dime = 0;
var nickel = 0;
var penny = 0;
var q = 0.25;
var d = 0.10;
var n = 0.05;
var p = 0.01;
var change = (moneyGiven - (cost + tax));
console.log(change);
while (change > 0) {
if (change >= q) {
change - q;
quarter++;
} else if (change >= d) {
change - d;
dime++;
} else if (change >= n) {
change - n;
nickel++;
} else if (change >= p) {
change - p;
penny++;
}
}; //while loop
console.log(quarter);
console.log(dime);
console.log(nickel);
console.log(penny);
The while loop is OK, the problem is in the statements
change - q;
change - d;
change - n;
change - p;
You are not updating the value of change. Change these to:
change = change - q;
change = change - d;
change = change - n;
change = change - p;

Categories