As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have some (I think they can be few stupid) questions...but they're questions, and I'd like post here, and debate it.
It's about conditionals. I'm developing in javascript, and I've some functions, and I'd like write optimal code.
For example, I have to check some conditions, so I can write:
if(text == ""){
//some code for finish
}else if(text== previousText){
//some code for finish
}
//here I write more code...which runs if both conditions had not complied.
My doubt is: what do you think is better to do?
Point 1
if(text == ""){
//some code for finish
}else if(text== previousText){
//some code for finish
}else{
//here I write more code...which runs if both conditions had not complied.
}
or
Point 2
if(text == ""){
//some code for finish using return
return;
}else if(text== previousText){
//some code for finish using return
return;
}
//here I write more code...which runs if both conditions had not complied.
I hope have explained well. Sometimes these things go unnoticed, but they're important, I think.
Thanks a lot, Daniel
I follow the rule to have only one return statement per function. So Point 1
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Working on a js app involving many objects, I want to be able to grab an object by a specific variable. Here is my code:
var pin = '0000';
$.each(employees, function(){
if(this.pin === pin){
curEmployee = this;
return false;
}
});
Though this approach works, I have a feeling that there are way better solutions out there... I was fiddling around with grep and tried:
var pin = '0000';
curEmployee = $.grep(employees, function(e,i){
return e[pin] === pin;
});
However, it is harder to determine a result, since now I will need to check the length to see if an array with provided back, and such.
Just looking for a best practices solution.
Since an Array is always returned from $.grep, just get the [0] index of the Array. If undefined, there was no match.
var pin = '0000';
curEmployee = $.grep(employees, function(e,i){
return e.pin === pin;
})[0]; // <--- always grab the first index
Without jQuery, you could use Array.prototype.filter in the same manner:
var pin = '0000';
curEmployee = employees.filter(function(e,i){
return e.pin === pin;
})[0];
You could always use jquery's filter method:
var pin = '0000';
curEmployee = $(employees).filter(function(e){
return e.pin === pin;
})[0];
In All honesty, IF you are using aloot of objects and performance is important...
.. and you want to avoid the asynchronous bugies in js.. you should go for a pure js. approach.
Something like for loop which god trough all objects. And possibly some result buffering. I know it is a pain in the ass and you probably wont like to do it. but it is the fastest way in most cases.
I personally use such aproach in a js. crm we are making, becouse the jquery way was a no go on 1000+ objects...
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want validate if username already exists while user insert the username in textbox or just after the textbox lost focus.
Jquery or Ajax?
Please, someone have examples of that?
In the onblur event of the text box, get the value of textbox and using jQuery ajax, make a call to a server page where you check it and return appropriate results. based on the results show the message to user (Available or Not Available)
Include jQuery in your page and have this script also
$(function(){
$("#txtUserName").blur(function() {
var userName=$(this).val();
if(userName!="")
{
$.get("checkusername.aspx?username="+userName+"&t="+$.now(),function(data){
if(data=="1")
{
$("#msgDiv").html("Not Available");
}
else
{
$("#msgDiv").html("Available :) ");
}
});
}
});
});
Assumuing checkusername.aspx page will read the querystring value and check in the database and return(Response.Write()) "1" or "0"
I prefer to use a Generic handler (.ASHX file) to do the server side checking instead of using the aspx file.
Both.
Use jQuery to react to the onblur event for the textbox. Then, use jQuery to make an ajax call to a controller to see if the user name is already taken.
$('#usernameTextBox').blur(function() {
alert('Make your ajax call here.');
});
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I was asked to implement the same question as this in an interview recently:
https://stackoverflow.com/questions/8447222/anagram-of-a-palindrome
I could not provide an answer but am interested to know the JavaScript solution.
That should be working..But I have tested it only with few inputs:) At least the theory behind it should be OK..
String.prototype.count=function(char) {
return this.split(char).length-1;
}
function isAnagramOfPalyndrom(string){
string.replace(" ", "");
var even = string.length % 2 == 0;
var flag = false;
for(var i = 0; i < string.length; i++){
if(string.count(string.charAt(i)) % 2 != 0){
if(even) return false;
else{
if(flag) return false;
flag = true;
}
}
}
return true;
}
Theoretically, if you have an even number of every letter except 1, it's an anagram of a palindrome.
See: "Kayak" k:2, a:2, y:1
See: "SAAS" s:2, a:2
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
So I have been spending a lot of time lately on codecademy and it is amazing. I feel like I am starting to really grasp JavaScript and programming in general. However, I want to start the basics of implementing my new skills into use. I am sure I am not alone with this desire and while I have some books coming in the mail (for jQuery, Ajax and Ruby) I thought Stack Overflow might be a good place to start.
Let's say for example I wanted to use the code (or something like it) below that I just wrote to calculate someones BMI and interpret it for them. It prompts them to input their weight and height and then calculates their BMI.
The code works in the codecademy scratch pad but I would have no idea how to put this code to use in the real world. I have a feeling it would have something to do with creating an HTML form and taking the input names as variables but I'm not sure.
If this isn't the correct place to ask the question, I apologize, this is my first question on Stack Overflow!
var weight = prompt("How much do you weigh, in pounds?");
var height = prompt("How tall are you, in inches?");
var BMI = (weight*703)/(height*height);
console.log("You have a BMI of " + Math.floor(BMI));
if (BMI<=15){
console.log("You are very severly underweight!");
}else if (BMI<16 && BMI>=15){
console.log("You are severly underweight!");
}else if (BMI<19 && BMI>=16){
console.log("You are underweight");
}else if (BMI<25 && BMI>=19){
console.log("You are healthy!");
}else if(BMI<30 && BMI >=25){
console.log("You are overweight" );
}else if(BMI<35 && BMI >=30){
console.log("You are moderately obese!");
}else{
console.log("lose weight fatty!");
}
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I'm looking for a simple function (javascript / jquery) that checks whether or not ANY contents of a textarea is selected or highlighted... the function needs to return true or false.
Thanks :)
Try this
function isTextSelected(input){
var startPos = input.selectionStart;
var endPos = input.selectionEnd;
var doc = document.selection;
if(doc && doc.createRange().text.length != 0){
return true;
}else if (!doc && input.value.substring(startPos,endPos).length != 0){
return true;
}
return false;
}
Usage
if(isTextSelected($('#textareaId')[0])){
//text selected
}
Demo