numbers and words not numbers to words js [closed] - javascript

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.

Related

Multiple conditions with logical AND operator 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 1 year ago.
Improve this question
I'm new to JavaScript and trying to implement a logical AND statement such that the field has a character limit greater than 0 and less than or equal to 100. Where's my try:
document.getElementById('order-content').onkeyup = () => {
if (document.getElementById('order-content').value.length > 0 &&
document.getElementById('order-content').value.length <= 100 ) {
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}
This is not working and the syntax is undoubtedly incorrect. Can anyone help me with the way to implement this properly?
Thanks!
You can try something like this:
The requirements of the length are length > 0 and length <= 100. The expression (length > 0 && length <= 100 ) will evaluate to true if the length passes the requirements. The exclamation point reverses the boolean.
document.getElementById('order-content').onkeyup = () => {
let length = document.getElementById('order-content').value.length;
if (!(length > 0 && length <= 100 )){
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}

Using == in javascript always false [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 1 year ago.
Improve this question
Hello I use a javascript function to change my HTML background color by DI state
here's my code
function pageData() {
var DI1_STATE =document.getElementById('DI1').textContent; //load DI1
console.log(DI1_STATE); //DI1_STATE= ON or OFF(TYPEOF = String)
console.log(DI1_STATE=='ON'); //ALWAYS FLASE
console.log(DI1_STATE=='OFF'); //ALWAYS FLASE
var result = DI1_STATE.localeCompare('ON'); //WORK preset 1(TRUE) or -1(FLASE)
console.log(result);
if (DI1_STATE == 'ON'){
document.getElementById('DI1').style.backgroundColor = 'Coral';
document.getElementById('DI1').style.color = 'White';}
else{
document.getElementById('DI1').style.backgroundColor = '#ccc';
document.getElementById('DI1').style.color = 'black';}}
I wonder why == is not work
the whole Html code
I made the server at a microchip,i update the "DI1" by getsensorDATA3()
and the server command below
You always have to check for line breaks, spaces or other non visible characters when comparing string values from html elements. Try
var DI1_STATE =document.getElementById('DI1').textContent.trim()
localeCompare is used to determine sort order and only reports back if the reference string comes before or after the comparison string (in the sort order):
Negative when the referenceStr occurs before compareString
Positive when the referenceStr occurs after compareString
Returns 0 if they are equivalent
You might use it like array.sort((a, b) => b.localeCompare(a));
Tests....
let test = document.getElementsByTagName('textarea')[0].value;
console.log('"' + test + '"')
console.log('test == "TEST"', test == "TEST") // false
console.log('test.localeCompare("TEST")', test.localeCompare("TEST")) // 1
test = test.trim();
console.log('test == "TEST"', test == "TEST") // true
console.log('test.localeCompare("TEST")', test.localeCompare("TEST")) // 0
<textarea>TEST </textarea>

Why does random number generator not show on html page? [closed]

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;
}

How to check for two conditions using && operator [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to use 1 If statement to check that both values are not zero
if ((minvalue !== 0) && (maxvalue !== 0)) {
// Both are not 0
}
else
{
// Both values are 0
}
I can get it to work by using two if statements
if ((minvalue !== 0){
if(maxvalue !== 0){
// Both values are not zero
}
}
But I'm not sure how to do it in one If.
This will also work
if (minvalue || maxvalue) {
// Both are not 0
}else {
// Both values are 0
}
Edit :
If you example doesn't work, you should consider doing
console.log(minvalue,maxvalue);
Your code works, so that's your minvalue and maxvalue which are wrong. Might be strings
Your first code block should be fine. If you want to get a bit more clever about it you could instead test that the product of both values is not zero (as anything multiplied by zero will be zero).
const minvalue = 1
const maxvalue = 3
if (minvalue * maxvalue !== 0) {
console.log('foo!') // foo!
} else {
console.log('bar...') // [not hit]
}
Also, stylistically, it's considered bad practice to leave hanging curly braces. Move your else and subsequent opening block curly brace up a line (as above).

jQuery quiz application how to implement a point based answering system? [closed]

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 6 years ago.
Improve this question
So I am reasonably new to javascript and jquery. I have been looking at tutorials which have been a good starting point however they all seem to have one question that is right, so they are able to check within a function if the users answer is correct via a if equal to statement for example.
if (answers[i] == userAnswers[i]) {
flag = true;
}
What I would like to do however, is have a different value attached to each of the potential answers for example
var 1 = 50
var 2 = 0
var 3 = 10
Question 1
var a = 1
var b = 2
var c = 3
var d = 3
Question 2
var a = 3
var b = 1
var c = 2
var d = 3
What would be the best way to do something like this?
First, an assumption:
answers[i] contains a number which is what the user picked as an answer to question i (Note: if answers[i] is in fact a character, you can get the integer value by subtracting answers[i] by the ascii value of 'a').
To do what you want to do, simply define a 2-dimensional array score[i][j], where i is the index of the question, and j is the option number (ie the asnwer to question i), then score[i][j] gives the score for this question. So let's say for question 1, the options are as you described above (ie a = good, b = wrong, ...), then you would have
//set the values for the answers
score[1][1] = good; score[1][2] = wrong; score[1][3] = wrong; score[1][4] = okay
score[2][1] = okay; score[2][2] = good; score[2][3] = wrong;
//more score setting
//get the value of the answer
var score_q1 = score[1][answers[1]]
var score_q2 = score[2][answers[2]]
ie, since answers[i] contains the value of the answer as a number, by calling score[question_nb][answers[question_nb]] you get the score for that question. In this case, there are no if statements. If you want to get the total score, just loop over all questions and sum up the score for that question.
The best way would be a key/value pair. I would try something like json. You would have your json with a key of good and a value (for points) as 50. The json would look something like below:
var kvPair = {"good":"50", "wrong":"0", "okay":"10"};
then when someone clicks an answer it would run ajax to determine the score:
$.ajax({
type: "POST",
dataType: "json",
url: "someUrl",
data: kvPair,
success: function (data) {
//do something with score data
},
error: function (event) {
ShowErrorLabel("ERROR in ajax call('someUrl'): \n" + "Error : " + event.status + " - " + event.statusText);
}
});

Categories