How do i fix this value code - javascript

So i want to get this working! Lets say value is within 93-120% of value2, i want the text to change to working or not working!
The issue is i do not know how i am able to do if value is greater than or equal to percentage of value2! I am sure this is an easy fix/line of code.
Thank you.
<html>
<body>
<p id="demo">Display the result here.</p>
<script>
var value = 30;
var value2 = 27;
if (value >= 93% "of lets say value2" && value <= 120% "of same value2") {
document.getElementById("demo").innerHTML = "Working";
} else {
document.getElementById("demo").innerHTML = "Not Working";
}
</script>
</body>
</html>

if (value >= 0.93*value2 && value <= 1.2*value2) {

What about this simple math:
if (value >= 93% "of lets say value2" && value <= 120% "of same value2")
if ( (value/value2) > 0.93 && (value/value2) < 1.2 )
Note:
No need to check for zero of if the values are set (not undefined).
Since if there is any error of this kind it will be NaN or Infinity

Related

How can i change a value in javascript?

I have the folowing code:
var price1 = (length1 * width1) * priceSM / 10000;
if (price1 < 10) {
alert("Size too small");
return;
}
document.getElementById('result1').innerHTML = Math.round(price1);
What I need to do:
If price 1 < 10 alert AND then price1 = 10, but if price1 = 0, or bigger than 10, that's fine.
length1 and width1 are INPUTS
Please help, I'm at very beginning with javascript.
I don't know if I understand well what you want.
Try this ?
var price1=(length1*width1)*priceSM/10000;
if(price1 < 10 && price1 != 0){
alert("Size too small");
price1 = 10;
return;
}
document.getElementById('result1').innerHTML=Math.round(price1);
If you want to update your element "result1" with price1 when < 10 then just remove "return;"
If it isn't what you want, please tell us and post all function's code please.
To avoid the alert when the price is zero, add that in the condition with the && operator:
price1 > 0 && price1 < 10
To set the price to 10 in that case, add this to the if block:
price1 = 10
To really show the modified price (10), you should remove the return statement from that if block. That way the code will continue to execute the last statement.
Finally, not really a problem, but it is better to use the textContent property instead of the innerHTML property when you intend to assign something that is plain text and not HTML.
So your code then becomes:
var price1 = (length1 * width1) * priceSM / 10000;
if (price1 > 0 && price1 < 10) {
price1 = 10
alert("Size too small");
}
document.getElementById('result1').textContent = Math.round(price1);
Using .value property you are able to set the value.
document.getElementById("myText").value = "Hello World...";

Javascript assiging multiple values to `ternary` operator

How to assign multiple values to ternary operator? is that not possible? I tried like this, but getting error:
size === 3 ? ( var val1=999, var val2=100; ) : 0;
and
size === 3 ? ( var val1=999; var val2=100; ) : 0;
above both approach throws error. how to set both var val1 and var val2;
I can directly declare it. But I would like to know the ternary operator approach here.
var size=3;
var val1=null;
var val2=null;
size === 3 ? ( val1=999,val2=100 ) : 0;
console.log(val1,val2)
Its a syntax error .You could use like this separate call
var size=3;
var val1 = size === 3 ? 999 : 0;
var val2 = size === 3 ? 100 : 0;
console.log(val1,val2)
You can do like this
var val1 = 0,
val2 = 0,
size = 3;
3 === size ? function() {
val1 = 999;
val2 = 100
}() : 0;
console.log(val1, val2);
Use an if statement.
var val1;
var val2;
if (size === 3) {
val1 = 999;
val2 = 100;
}
Whilst D-reaper's answer answers your question, really your question is not the right thing to ask. (Additionally eval is useful very very occasionally but is otherwise considered "evil" as it prevents various JavaScript engine optimisations and opens security holes like XSS if used on stored user input data).
Ternary operators are useful when you're using them to assign to another variable like:
var val1 = size === 3 ? 999 : 0;
In your example the fact you do no assignment, the first expression does not intend to return a value and the second value of 0 is ignored and therefore redundant is a very strong code smell they should alert you to there being a better easier way of doing what you want.
The syntax of ternary operator is
condition ? expr1 : expr2
var val1=999; var val2=100; is a valid declaration (var val1=999, var val2=100; is not), but NOT an expression. So you can't use them the way you've done it in your code. However, you can make it into an expression by using the eval function like so:
size === 3 ? eval('var val1=999; var val2=100;') : 0;
Of course, as the others have pointed out. Using eval is the wrong approach to take. I am showing you how it could be done for the sake of answering your question.
Here is my try: it works fine for me:
createDigit : function( size ) {
var val1, val2;
size === 3 ? ( val1=999, val2=100 ) : size === 2 ? ( val1=99, val2=10 ) : 0;
//generates only 3 digit values
return Math.floor( Math.random()*(val1-val2+1 )) + val2;
},
I see what you are trying to do, you can use Math.pow() to generate numbers instead of checking on size manually. I have put down two methods below : createDigit is mine which can generate numbers for any size given using Math.pow() and createDigitBuggy is yours which will just generate numbers for size 2 and 3 and rest will be NaN.
// Improved version
const createDigit = (size) => {
if (size > 0) {
const val1 = Math.pow(10, size) - 1
const val2 = Math.pow(10, size - 1)
return Math.floor(Math.random() * (val1 - val2 + 1)) + val2
}
return 0
}
// Old buggy version
const createDigitBuggy = (size) => {
var val1, val2
size === 3 ? (val1 = 999, val2 = 100) : size === 2 ? (val1 = 99, val2 = 10) : 0
return Math.floor(Math.random() * (val1 - val2 + 1)) + val2
}
console.log(createDigitBuggy(1)) // prints NaN
console.log(createDigitBuggy(2)) // prints number
console.log(createDigitBuggy(3)) // prints number
console.log(createDigitBuggy(4)) // prints NaN
console.log(createDigit(1)) // prints number
console.log(createDigit(2)) // prints number
console.log(createDigit(3)) // prints number
console.log(createDigit(4)) // prints number

Javascript Function defaulting to wrong answer (I think)

Hi can somebody tell me why the output to my function defaults to even when you insert over 17 numbers? It's probably super simple, please go easy on me!
function oddOrEven(number) {
var number = document.getElementById('number').value;
if(number % 2 != 0) {
document.getElementById('demo').innerHTML = "Odd";
}
else {
document.getElementById('demo').innerHTML = "Even";
}
if (number.length === 0) {
document.getElementById('demo').innerHTML = "Odd / Even";
}
}
You can simplify this whole thing. If you are always grabbing the input with id 'number' you don't need to pass a param, and then after a simple test you can inline the answer you want:
function oddOrEven(){
var val = document.getElementById('number').value;
var number = parseInt(val, 10);
// if it's not a valid number, you'll have NaN here which is falsy
if (number) {
document.getElementById('demo').innerHTML = (number % 2) ? "Even" : "Odd";
}
}
All that said, I just caught that you're talking about 17 digits (thanks to #JJJ's comment) rather than using the function more than once. The problem in this case is that JS integers have a size limit. If you parse anything larger it returns a number you're not going to expect. There are a lot of discussion of general handling of very large numbers here: http://2ality.com/2012/07/large-integers.html, but for your modulus problem you could take the last digit and check if that's odd or even like so:
function oddOrEven(){
var val = document.getElementById('number').value;
var number = parseInt(val, 10);
// if it's not a valid number, you'll have NaN here which is falsy
if (number) {
var lastDigit = val[val.length-1];
document.getElementById('demo').innerHTML = (parseInt(lastDigit, 10) % 2) ? "Even" : "Odd";
}
}

Printing the next 10 even and odd numbers ( java-script)

1) If I enter an even number, I want next 10 even numbers to be printed. If I enter an odd number, I want next 10 odd numbers to be printed.
2)If I enter an even number, I want previous 5 even numbers to be printed. If I enter an odd number, I want previous 5 odd numbers to be printed.
i am newbie to programming and trying to learn java-script myself, the above is the question i am trying to solve. i am confused, i am not sure how to make the code to write the next 10 odd even number (i am referring to the first question).also the previous 5 (referring to second question).. below is my starting attempt. i am stuck
function isEven {
var value = prompt("");
if (value % 2 == 0) {
for (var i = 2; i <= ; i = i + 2;)
document.write(i + "<br>");
}
}
isEven();
Answer 1:
if(number>=0){
for(i=2;i<21;i+=2){
console.log(number+i);
}
}
Answer 2:
for(i=2;i<11;i+=2){
if((number-i)>=0){
console.log(number-i);
}
}
1) If I enter an even number, I want next 10 even numbers to be printed. If I enter an odd number, I want next 10 odd numbers to be printed.
function function1() {
var value = prompt("");
value = parseInt(value);
for (var i = 1; i <= 10; i = i + 1){
value = value + 2;
document.write(value + "<br>");
}
}
2)If I enter an even number, I want previous 5 even numbers to be printed. If I enter an odd number, I want previous 5 odd numbers to be printed.
function function2() {
var value = prompt("");
value = parseInt(value);
for (var i = 1; i <= 5; i = i + 1){
value = value - 2;
document.write(value + "<br>");
}
}
Just to clarify. You want to print both the previous 5 numbers and next 10 numbers of same 'evenness' for any given number?
In which case, you should do just that... You dont need to care if the number is even or odd, because the next/previous is always 2 away. (What you do when you cross 0 is up to you)
for (var i = 1; i <= 5; i++)
document.write((INPUT - (i*2)) + "<br>");
for (var i = 1; i <= 10; i++)
document.write((INPUT + (i*2)) + "<br>");
refer this woking demo. hope this will help to you.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<style type="text/css">
</style>
<body>
Enter a number : <input type="number" id="num">
<button id="butp" class="chk">print previous five numbers</button>
|| <button id="butn" class="chk">print next ten numbers</button>
<ul id="print">
</ul>
<script type="text/javascript">
$(".chk").click(function(){
var the_id = $(this).attr('id');
//alert(the_id);
var theVal = parseInt($("#num").val());
if (the_id =="butp") //this means user asking for previous
{
if (theVal==0 || theVal < 10)
{
alert("cannot continue the operation, please enter a valid nubmer to continue");
}
else
{
for (var i=1;i<6;i++)
{
newVal = theVal - (i*2);
$("#print").append($("<li>"+newVal+"</li>"));
}
}
}
else // this means user asking for next
{
for (var i = 1;i<11;i++)
{
if (theVal==0)
{
alert("please enter a valid number to continue");
}
else
{
newVal = theVal + (i*2);
$("#print").append($("<li>"+newVal+"</li>"));
}
}
}
});
$("#num").on('change keyup keydown', function(){
theVal = $(this).val();
if (theVal == "")
{
$("#print li").css({"display":"none"})
}
})
</script>
</body>
</html>
function evenOdd(value) {
if(value%2==0){
console.log(`it's and even number ${value} next 3 digit will be`);
for (var i = 1; i <= 10; i = i + 1){
value = value + 2;
console.log(value);
}
}else{
console.log(`it's and odd number ${value} next 3 digit will be`);
for (var i = 1; i <= 10; i = i + 1){
value = value + 2;
console.log(value);
}
}
}
evenOdd(13)

How to display an integer into a div

I am working on getting the user t input a number for something in this case it would be a resistor value, I have asked them via a prompt to pick a number and then if that number is within the limits I have set then the number would be displayed back to the user within a div.
I have built this already and got it to work with a couple of test messages so I believe the function itself is fine however I am having a problem that whenever the user enters a correct value that value isn't displayed but "undefined" is displayed instead.
This is the HTML I am testing,
<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>
And this is the JavaScript function
function R1Value() {
var R1ValueEntered = prompt("please enter a value for R1:")
var R1 = parseInt(R1ValueEntered)
var display = document.getElementById('test3');
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1.value;
}
else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
}
I have placed into into a jsfiddle as even though there isn't a lot of code it may save you some time if you can have a look, http://jsfiddle.net/2ufnK/72/ I may be missing something simple but I can't seem to fix the problem.
Just remove .value :
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
}
FIDDLE
.value is undefined for integers. Remove that and your code will work fine.
You don't need the R1.value. Just calling R1 will return it's value.
you got an error here, you assign R1 value, not R1.value
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
}
I tried your code.
Besides that you might have missed a closing tag, it worked for me. You need to change these lines:
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1.value;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
to:
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
you don#t need to get the value of R1, because it already IS hte Value.
I hope i could help.
regards
Your problem is that you are never closing your <div> and you also call R1.value, which is basically calling .value on an integer, which is undefined. Try the following:
function R1Value() {
var R1ValueEntered = prompt("please enter a value for R1:")
var R1 = parseInt(R1ValueEntered)
var display = document.getElementById('test3');
if (R1 >= 120 && R1 <= 1000000) {
display.innerHTML = R1;
} else {
alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}
}
<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>

Categories