Alerting on the last iteration of a Loop [closed] - javascript

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 2 years ago.
Improve this question
Hi So ive read threw a whole bunch of the questions in regards to this.. Most of them throw off my data or I just cant get them to actually proc on the last iteration
for(const[m, value] of datesLess) {
console.log("Logging M:" + m);
if(startD == m || endD == m){
console.log(m)
takendates.push(m);
if (m === datesLess.length - 1) {
alert("The following Bookings are taken:"+takendates.join("\n"));
}
}else{
this.logentry(startDay,startMonth,startYear,endDay,endMonth,endYear,entryinfo);
}
}
In essence im trying to display the alert on the last iteration of the loop so the user can see which dates are incorrect as to just displaying a popup with no details

I'm fairly sure what you really want to be doing is showing the alert after the loop
const takenDates = [];
for(const[m, value] of datesLess) {
console.log("Logging M:" + m);
if(startD == m || endD == m){
console.log(m)
takendates.push(m);
}else{
this.logentry(startDay,startMonth,startYear,endDay,endMonth,endYear,entryinfo);
}
}
if(takendates.length) {
alert("The following Bookings are taken:"+takendates.join("\n"));
}

Related

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>

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).

Error in my 'if' statement containing break (wrox ch 3 exercise 4) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
Trying to figure out this times table function...It's not working as there seems to be a problem with the if statement containing break. Any help would be much appreciated.
function writeTimesTable (startNumber, endNumber, multNumber) {
for (;startNumber <= endNumber; startNumber++) {
console.log(startNumber + " * " + multNumber + " = " + startNumber * multNumber + "</br>")
}
}
/* writeTimesTable(3,4,5) */
var timesTable;
while ( (timesTable = prompt("Enter the times table", -1)) != -1)
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
};
if (timesTable == -1) {
break;
};
console.log("<br />The " + timesTable + " times table<br/>)");
writeTimesTable(timesTable, 1, 12);
you're not using brackets in your outer loop. without brackets only the first statement is executed in the loop.
while ( (timesTable = prompt("Enter the times table", -1)) != -1){
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
}

Syntax error when trying to test for empty string [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 8 years ago.
Improve this question
In Javascript, I am trying to do a simple task of testing and returning the longest of two words. If the words are empty then the return should say "empty string". I keep getting a syntax error when testing for an empty string with the or elseif statement. I am using (!word1) and (!word2) because my understanding is in Javascript it is a boolean statement and should be false. Please tell me where I am going wrong:
function longest(word1, word2) {
if (word1.length >= word2.length) {
return (word1);
} else {
return (word2);
} else if (!word1) || (!word2) {
return "an empty string";
}
}
console.log(longest('hi'));
if conditions should be surrounded by exactly 1 pair of parentheses.
Change else if (!word1) || (!word2) to else if (!word1 || !word2).
Your "else" clause should be the last clause in your if statement:
function longest(word1, word2) {
if (word1.length >= word2.length) {
return (word1);
} else if (!word1 || !word2) {
return "an empty string";
} else {
return (word2);
}
console.log(longest('hi'));
You got your else and else if around the wrong way. Heres the complete code for your problem.
function longest(word1, word2) {
if ((!word1) || (!word2)) {
alert ("an empty string");
} else if (word1.length >= word2.length) {
alert (word1);
}else{
alert (word2);
}
}
longest('hi');

numbers and words not numbers to words js [closed]

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.

Categories