Calculator Script or Plugin - javascript

I'm looking for the Calculator Plugin (for Wordpress) or Script which should work in such a way:
Should be only one field where user can type the number of my product (Instagram Likes). And depending on the number the cost changes.
I'v got the variety of prices for different Likes number. For example:
1-10000 likes = 0,0009 per 1 like
10001-50000 = 0,0008 per 1 like etc
And if the user type the number 5555 it should automatically count 5555*0.0009
Could you, please, help me?

var likes = document.getElementById("your id for likes").value;
var costPerLike;
if(likes <= 10000){costPerLike = 0.0009} else if(likes >= 10001 && likes <= 50000){costPerLike = 0.0008}
i think this should help you get in the right direction.
you probably want this in an onchange or onsubmit event.

You can use this one.
function priceCalculation(a){
if(a <= 10000){
return 0.0009;
}else if(a >= 10001 && a <= 25000 ){
return 0.0008;
}else if(a >= 25001 && a <= 50000 ){
return 0.0007;
}else if(a >= 50001 && a <= 100000 ){
return 0.0005;
}else{
return 0.0004;
}
}
$('#likecount').keyup(function(){
var price = priceCalculation($(this).val());
console.log(price)
var total = $(this).val() * price;
$('#output').text(total.toFixed(4));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="likecount" />
<p>Price: <span id="output"></span></p>

Related

java script guessing between 100 and 0

hello i am very new to coding and i have no clue on how this codes does not work.
what i need to do is make a random number(the test value is 20) from 0 to 100 and store it in a value then the user must guess it if its higher then the random value the code will say to high and if too lower then it will say to low and if they type -1 or get the number right the code will then break and stop the loop
the problem: i dont know how to loop the code back to the start so it can ask the user the same question
the code its self:
var min = 0
var max = 100
var roll = 20
function start() {
println("hello pick a num between 0 and 100 type -1 if you wanna quit")
var roll = 20
var userguess = readInt("what is your guess: ")
while(userguess > min){
if (userguess > roll){
println("too high you ")
}
if (userguess < roll){
println("to low ")
}
if(userguess == -1 ){
println(" you quit")
break;
}
if(userguess == roll ){
println("wait you got it ")
break;
}
}
}
welcome to the coding world.
In this case, you can use Recursive Functions. If you Google it, you’ll find tutorials that can explain it much better than my answer.
I'll just implement your requirements.
var min = 0
var max = 100
var roll = 20
function start(min, max, roll) {
println("hello pick a num between 0 and 100 type -1 if you wanna quit")
var userguess = readInt("what is your guess: ")
if(userguess == -1 ) {
println("you quit")
return "User exited";
}
if(userguess == roll ) {
println("wait you got it")
return userguess;
}
if(userguess < min && userguess > max) {
println("out of range")
return start(min, max, roll)
}
if (userguess > roll){
println("too high you")
return start(min, max, roll)
}
if (userguess < roll){
println("to low")
return start(min, max, roll)
}
}

Grading Students in JS with recursion - Range Error

I was trying to work on this hackerrank problem.
Every student receives a grade in the inclusive range from
0-100 to .
Any less than 38 is a failing grade.
Sam is a professor at the university and likes to round each student's according to these rules:
If the difference between grade the and the next multiple of
5 is less than 3, round up to the next multiple of 5. If the value of grade is less than 38, no rounding occurs as the
result will still be a failing grade.
Given the initial value of for each of Sam's students, write code to
automate the rounding process.
My code is:
function gradingStudents(grades) {
const roundup = y => y + 1;
{
if ( grades < 38 || grades % 5 === 0) return grades;
else if ( grades % 5 < 4 && grades % 5 !== 0) return roundup(grades);
}
{
if (roundup % 5 === 0) return roundup;
else { gradingStudents(roundup + 1) }
}
}
gradingStudents(38) // -> 39
I tried to use Math.ceil(grades) inside the variable roundup but output didnt change. So, when you invoke the function with a number that is not before a multiple of 5 (e.g. 43) it returns the proceeding number. However, if it is the number before a multiple of 5 it gives a range error. "maximum call stack size reached."
As far as I got, the code doesnt proceed to the second part. Even if it did, I am not sure if it would fetch the current value of the function roundup when dealing with if statements in the second block.
What do I dont get in here?
Also, this is actually meant for an array output but since I am a beginner I am pretty much okay with this one for the start as well :D .
Javascript solution:
function gradingStudents(grades) {
return grades.map((grade) => {
if (grade > 37) {
const offset = 5 - (grade % 5);
if (offset < 3) {
grade += offset;
}
}
return grade;
});
}
Try this:
function gradingStudents(grades) { //input: 43
var finalGrade;
if(grade < 38)
return grades;
else{
var gradeDif = grades % 5; //3
if(gradeDif > 3){
return grades;
}
else {
return grades + (5 - gradeDif); //Output: 45
}
}
}
One solution calculates the next multiple of 5 no bigger than the grade and uses that value to test whether or not to round up (next5 - grade < 3).
We write a simple function to round an individual grade and then for a list of grades use .map with that function.
const roundGrade = (grade) => {
const next5 = 5 * Math.ceil (grade / 5)
return (grade < 38) ? grade : (next5 - grade < 3) ? next5 : grade
}
const gradingStudents = (grades) =>
grades .map (roundGrade)
console .log (gradingStudents ([73, 67, 38, 33]))
Note that, like most solutions to this problem, no recursion is needed.
1-Use the Array.prototypt.map according to the question logic.
const gradingStudents = (grades) => grades
.map(n => (n >= 38 && n % 5 >= 3)?(n + ( 5 - ( n % 5 ) )):n )
let result = gradingStudents([0,25,38,56,89,77,78,57,58])
console.log(result)
My solution is this
function gradingStudents(grades) {
grades.map((grade,i)=>{
if(grade >= 38){
let fg = (grade/5).toString().split('.');
if(fg[1]>5){
grades[i]=((parseInt(fg[0],10)+1) * 5);
};
}
});
return grades;
}
console.log(gradingStudents([73,67,38,33]));
my solution:
function gradingStudents(grades) {
return grades.map(function(grade) {
return (grade >= 38 && grade % 5 >= 3) ? grade + 5 - (grade % 5) : grade;
});
}

Javascript - Calculate and compare

I'm learning JS, and in my practice I can't get the second part of my script to work for me.
I have 3 inputs (#ancho - #fondo - #alto), where I write integers number for a multiplication between them, followed by a division. This part works fine
Now the problem:
In the second part, I want to compare if the value "r =" is greater or less than the number written (por the user) in the input #peso, but it doesn't work for me.
What am I doing wrong here?
See the DEMO in JSFiddle
Thanks in advance!
HTML:
<input id="ancho"> <!-- Example value="22" -->
<input id="fondo"> <!-- Example value="20" -->
<input id="alto"> <!-- Example value="16" -->
<input id="peso"> <!-- Example value="3" -->
<div id="resultado"></div>
<div id="hacercalculo" onclick="calcular();comparar()">Calcular</div>
<!--css display none-->
<div id="uno">1</div>
<div id="dos">2</div>
<div id="tres">3</div>
jQuery:
https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js
Script:
// Calculate - This part works fine
function calcular(){
v1 = document.getElementById("ancho").value;
v2 = document.getElementById("fondo").value;
v3 = document.getElementById("alto").value;
r = v1*v2*v3/4000;
//document.getElementById("resultado").value = r;
document.getElementById("resultado").innerHTML = r;
};
//=============
// Compare - This, It does not work for me
function comparar() {
var camp1= document.getElementById("resultado");
var camp2= document.getElementById("peso");
//if (camp1.value >= 0 && camp2.value <= 3) {
//if (camp1.innerText >= 0 && camp2.innerText <= 3) {
//if (camp1.innerHTML >= 0 && camp2.value <= 3) {
if (camp1.innerText >= 0 && camp2.value <= 3) {
$("#uno").show(); // This, It does not work for me
}
};
Just a small amendment to "if statement" in your comparar() function.
textContent has to be converted to number.
if (Number(camp1.textContent) >= 0 && camp2.value <= 3) {
$("#uno").show();
}
Please refer to this code pen example after this change,
https://codepen.io/SathishVel/pen/KKVMvqj
If r is greater than or less than peso then r != peso. The following comparar function should handle the second part.
function comparar() {
var camp1= document.getElementById("resultado").innerHTML;
var camp2= document.getElementById("peso").value;
if (Number(camp1) != Number(camp2)) {
$("#uno").show(); // condition met hence #uno rendered.
}
};
There was some issues with your code hence why i didn't work
first you should get the value using textContent
var camp1= document.getElementById("resultado").textContent;
second you are retrieving the value twice
var camp2= document.getElementById("peso").value;
if (camp1 >= 0 && camp2.value <= 3)
it should be (camp1 >= 0 && camp2 <= 3)
finally for your code to work your if statement has to return a value
if (camp1.innerText >= 0 && camp2.value <= 3) {
return console.log('..show one') // use the return keyword to return value
}
try this
function comparar() {
var camp1= document.getElementById("resultado").textContent;
console.log(camp1)
var camp2= document.getElementById("peso").value;
console.log(camp2)
if (camp1 >= 0 && camp2 <= 3) {
$("#uno").show();
}
}
</script>
From what you said you need to check if the values ​​are in a certain range and also want to check if it has decimal places or not, so I will create two functions that do these two checks.
Function to check if a value is in a certain range:
function interval(a, b, c) {
return ((b - a) * (b - c) <= 0)
}
Example of use:
interval(2, 5, 6) // true (2 < 5 < 6)
interval(2, 1, 6) // false (2 < 1 < 6), false because 2 < 1 is false
interval(2, 7, 6) // false (2 < 7 < 6), false because 7 < 6 is false
Font: Check if a value is within a range of numbers credits Alexander
Function to check if the number has decimal places:
function isDecimal(a) {
if(Number(a) == Math.floor(a)) {
return false
} else {
return true
}
}
Example of use:
isDecimal(2) // false
isDecimal(2.2) // true
Note: In your algorithm you are using the following variables:
var camp1= document.getElementById("resultado");
var camp2= document.getElementById("peso");
They are in string format, so you need to transform them into numbers before using them in these functions, so below them do:
camp1 = Number(camp1)
camp2 = Number(camp2)

Calculating unit price based on quantity entered

I'm trying to create a script that displays a price in one box based on the number of employees entered in an adjacent box.
The script works fine for a static amount, i.e. the base price is £8 per employee. If 5 is entered the price displayed is correct, £40.
The problem I'm having is changing the unit price when a certain threshold is met. The unit price for 1-5 employees is 8, 6-21 is 7 and 22-50 is 6.
No matter what is entered in the employee box, the number is always multiplied by 8.
I'm very inexperienced when it comes to javascript so any help will be very welcome!
Here is my code:
<div class="employee-button">
<form name="calculator" id="calculator">
<input name="employees" id="employees" type="text" placeholder="# of Employees" />
</form>
</div>
<div class="cost-button">
<span id="result">Monthly Cost</span>
</div>
Javascript
$(function() {
var ccNum;
$('#employees').keyup(function(){
ccNum = $(this).val();
});
if((ccNum >= 0) && (ccNum <= 5)){
var unitPrice = 8
}
else if((ccNum >= 6) && (ccNum <= 21)){
var unitPrice = 7
}
else if((ccNum >= 21) && (ccNum <= 50)){
var unitPrice = 6
}
else var unitPrice = 8;
$('#employees').keyup(function(){
$('#result').text(ccNum * unitPrice);
});
})
Put everything within the keyup function. Your code wasn't working because the logic to calculate the price is only executed once, not for every keyup event.
$('#employees').keyup(function(){
var ccNum = $(this).val();
if((ccNum >= 0) && (ccNum <= 5)){
var unitPrice = 8
}
else if((ccNum >= 6) && (ccNum <= 21)){
var unitPrice = 7
}
else if((ccNum >= 21) && (ccNum <= 50)){
var unitPrice = 6
}
else var unitPrice = 8;
$('#result').text(ccNum * unitPrice);
});
In addition to putting the code inside your event handler, you do not need to keep declaring the same variable over and over. Also the test for 0 to 5 is not needed (as already covered by the default value):
$('#employees').keyup(function(){
var ccNum = ~~$(this).val();
// Start with the default
var unitPrice = 8;
if((ccNum >= 6) && (ccNum <= 21)){
unitPrice = 7;
}
else if((ccNum >= 21) && (ccNum <= 50)){
unitPrice = 6;
}
$('#result').text(ccNum * unitPrice);
});
Note: ~~ is a shortcut conversion to an integer value.

Error in JavaScript return code?

Here is the javascript code:
There is an error in code where nightSurcharges is added to total cost even if pickUptime is less than 20.
function TaxiFare() {
var baseFare = 2;
var costPerMile = 0.50;
var nightSurcharge = 0.50; // 8pm to 6am, every night //its flat 0.50 and not per mile
var milesTravelled = Number(document.getElementById("miles").value) || 0;
if ((milesTravelled < 1) || (milesTravelled > 200)) {
alert("You must enter 1 - 200 miles");
document.getElementById("miles").focus();
return false;
}
var pickupTime = Number(document.getElementById("putime").value) || 0;
if ((pickupTime == "") || (pickupTime < 0) || (pickupTime > 23)) {
alert("The time must be 0-23 hours");
document.getElementById("putime").focus();
return false;
}
var cost = baseFare + (costPerMile * milesTravelled);
// add the nightSurcharge to the cost if it is after
// 8pm or before 6am
if (pickupTime >= 20 || pickupTime < 6) {
cost += nightSurcharge;
}
alert("Your taxi fare is $" + cost.toFixed(2));
}
I want nightSurcharge to be added only when pickupTime is >=20, but that's not working right now.
Any help is appreciated. Thanks
This seems obvious to me.
if (pickupTime >= 20 || pickupTime < 6) {
cost += nightSurcharge;
}
This code right here adds nightSurcharge to the cost if pickupTime is greater than or equal to 20, OR less than 6. So of course it's added if it's less than 6.
if (pickupTime >= 20) {
cost += nightSurcharge;
}
Now it will only add to it if it's greater or equal to 20.
your code is:
if (pickupTime >= 20 || pickupTime < 6)
so if pickupTime is less then 6 it'll enter the if as well
http://jsfiddle.net/7rdzC/

Categories

Resources