Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The following code is resulting in 32 each time whether the input is 9 or 90 how can I fix it?
var celsius = document.getElementById("cel").value;
var fahrenheit = document.getElementById("far");
var btn = document.getElementById("con");
function convert() {
var frnht = celsius * 9/5 + 32;
fahrenheit.innerHTML = frnht;
}
btn.addEventListener("click", convert, false);
Put celsius definition inside your function.
var celsius = document.getElementById("cel").value;
Celsius is using the same initial value every time which would probably convert to 0 and hence result becomes 32. Adding this inside click handler will call this every time and assign a value depending on the input.
var btn = document.getElementById("con");
function convert() {
var celsius = document.getElementById("cel").value;
var fahrenheit = document.getElementById("far");
var frnht = celsius * (9/5) + 32;
fahrenheit.innerHTML = frnht;
}
btn.addEventListener("click", convert, false);
Try this, the value are initialized only when the event handler is triggered
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to get a random number of customers to "have gone on the website today". Why does the number not show on the page. I am sorry if this is something obvious but I am very new.
var customerNumber = document.getElementById("customer-number")
customerNumber.textcontent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
element.textcontent => element.textContent
Also you should prob change return(i) to return i;, both work but the first one makes it seem like return is a function.
You need to use textContent instead of textcontent :
var customerNumber = document.getElementById("customer-number")
customerNumber.textContent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
Try doing:
var customerNumber = document.getElementById("customer-number");
customerNumber.innerHTML = randomCustomerNumber();
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36);
return i;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have the following code:
// Reference to the <div> which displays the random number:
var rndDiv = document.getElementById('rndNum')
// Reference to the <button> which generates the random number:
var rndBtn = document.getElementById('rnd')
// Generating the random number through 'click' eventlistener:
rndBtn.addEventListener('click', function intRnd() {
var n = Math.floor((Math.random() * 10) + 1);
console.log(n)
rndDiv.innerHTML = n
})
how could/should i write this code differently, how would you write it? Would you use, for example, arrow functions? let instead of var? I'm just curious. Also i'm the total opposite of a 'pro'-coder, just a beginner, and would like to read your code to this solution.
Thanks for taking your time and reading my post!
Here you go ... !
IIFE
Arrow Function
Let
(function() {
let rndBtn = document.getElementById('rnd');
rndBtn.addEventListener('click', () => {
let rndDiv = document.getElementById('rndNum');
rndDiv.innerHTML = Math.floor((Math.random() * 10) + 1);
});
})();
<button id="rnd">Click</button>
<div id="rndNum"></div>
Here is another way
const randomNumGenerator = () => Math.floor((Math.random() * 10) + 1);
const randomNumDiv = document.getElementById('rndNum');
document.getElementById('rnd').addEventListener('click', () => {
randomNumDiv.innerHTML = randomNumGenerator();
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to display decimal value in calculator using javascript.
if i click any buttons in the calculator, the button value added to decimal value from the right to left and it should be display in the screen.
For Eg: The default value is 0.00 if i click 2, it should be 0.02 and if i click 3, it should be 0.23 and if i click 4, it should be 2.34 and if i click 5, it should be 23.45 and so on.
You could try:
let number = 0;
// Returns the new value in case you need it to display in console
// and updates the number variable with the new value.
function addNumberRightToLeft(value) {
number = ((number * 10) + (value / 100)).toFixed(2);
return number;
}
console.log(addNumberRightToLeft(5)); // Shows '0.05' in console and updates number variable, so it is now '0.05'.
// or
addNumberRightToLeft(5); // Does not print to console but updates number variable, so it is now '0.55'.
or even use ES6 arrow functions and without side effects (as suggested by Nika):
const addNumberRightToLeft = (p, v) => ((p * 10) + (v / 100)).toFixed(2);
where p is the last value returned and v is what you want to add. In action:
let number = 0;
number = addNumberRightToLeft(number, 5); // 0.05
number = addNumberRightToLeft(number, 5); // 0.55
Like below: resultNum should be first initialize as 0.00;
function showCalc(inputNum)
{
var a = resultNum * 10.00;
var b = inputNum / 100;
resultNum = a + b;
return resultNum;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am learning javascript, now I'm trying to do mini-coding challenges. I cannot figure out what needs to change:
let roll = function() {
let roll1;
let roll2;
roll1 = parseInt(Math.random() * 6) + 1;
return roll1;
roll2 = parseInt(Math.random() * 6) + 1;
return roll2;
document.getElementById('dice').innerHTML = roll1 + "and " + roll2;
}
Your function roll is returning your roll1 and roll2 before it gets to update the html. Upon being executed return roll1 exits the function and returns the value of role1. The lines of code after it never get called. What you need to do is just remove the returns from the function. when getElementById() is called that will, in a way, act as you returning the variables because it will update the html to display your result.
let roll = function() {
let roll1;
let roll2;
roll1 = parseInt(Math.random() * 6) + 1;
roll2 = parseInt(Math.random() * 6) + 1;
document.getElementById('dice').innerHTML = roll1 + "and " + roll2;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple code gotten from the internet and it did not answer what I really wanted as output. I have two input fields; one for the input and another for the output and they are processed through this function:
<script type="text/javascript">
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value
if (x >= "100") {
document.getElementById("eventlog").value = "" +
return x = ['Generalities'];
}
}
</script>
What I'm really needing is that when I enter numbers below 100, output must be Generalities. I haven't got it correctly. And I went here to ask some help. Thanks.
You're never outputting your value back into the output field. All you're doing is returning the value. You need to set the value of your output field to "Generalities".
Example
var input = document.getElementById("onkeyup").value;
// You should be giving your elements meaningful IDs.
if(+input < 100) {
document.getElementById("output").value = 'Generalities';
// Assumes an output field called "output".
}
Try this:
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value;
if (x < 100){
document.getElementById("eventlog").value = "Generalities";
}
}
I see a few errors. Check this out for comparison:
function AnEventHasOccurred() {
// should probably save the elements to variables
// since you'll be checking and changing the values
var x = document.getElementById("onkeyup");
var y = document.getElementById("eventlog");
// should be 100, not "100"
if (x.value < 100) {
y.value = "Generalities";
} else {
y.value = "";
}
}
This should work fine. Check it out on jsfiddle.
More Recommendations
Your return statement doesn't correspond with your "output": it
does nothing valuable in this case.
You check or set the value of an input by getting the element and
using its value key.
You should put semi-colons at the end of most javascript lines, with the exceptions generally being curly brackets {}, comments // and /* */, and empty lines.