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 6 years ago.
Improve this question
I was testing my answer to another SO question and came across this weird behavior, for the life of me I don't know what is up.
Code:
function translateLetter(input) {
const untranslated = "abcdefghijklmnopqrstuvwxyz";
const translated = "zyxwvutsrqponmlkjihgfedcba";
var i = untranslated.indexOf(input);
console.log(i);
return translated.substring(i,1);
}
console.log(translateLetter("a"));
console.log(translateLetter("b"));
console.log(translateLetter("c"));
Expected output:
0
z
1
y
2
x
Actual output:
0
z
1
<--- WTH?
2
y <--- WTF?
Code on JSFiddle
If speed is important, I'd use an object to do your lookup.
eg.
var translateLetter= {a:'z',b:'y'... etc }
and then you can simply do ->
console.log(translateLetter['a'])
Use .substr() for a length. .substring() takes a position.
The first empty space is a null because you have passed "b" in the function on which indexOf function returned 1 which is then set to variable "i" and i is then used in substring function and substring fuction return the value which is in the middle including the first and excluding the last index of the values given so substring got (1,1) and 1,1 are pointing the same index so it returned null.
same for the second.
substring(Begin index inclusive,last index exclusive);
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 1 year ago.
Improve this question
So I have a function that will be called with a 1 word string.
Test("ABCD")
my code:
function Test(x)
var str=""
str.push($)
x.toLowerCase()
return str
//So I want the output to change the "ABCD" into "abcd$"
You are accepting the argument x in the function and running toLowerCase on it. The output is not having any value from the input.
Also there is no push method defined for string. It can be String.concat. Even that is not needed, you could make use of arithematic + itsel for concatenation or string leterals.
Just lowercase the input append a $ symbol. Its done!!
Im making use of .toString() method aswell. To ensure code is not breaking for other input types
It should be
function Test(x) {
return x.toString().toLowerCase() + '$'
}
console.log(Test("ABCD"));
Or Simply.
Test = (x) => `${ x.toString().toLowerCase() }$`;
console.log(Test("ABCD"));
This is just an example, please change the text which you want to add at end as you wish.
I passed the string in function through console.log but you can call the function as you wish without console.log.
function capFirst(str) {
return str.toLowerCase().concat('', '$');
}
console.log(capFirst('ABCD'));
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
I scrape info from webpage and I want to push them to array in order to use them later. But when I try to reach 1st, 2nd etc... item, instead of the word I got back only a character.
var arrType = [];
$('[name="type"]> option').each(function () {
arrType.push($(this).text());
});
const vehicleType = arrType.join(", ");
If I print the vehicleType then I got something what is looks like array (also the typeof is array), but when I want to print out vehicleType[0] I just get back one character.
console.log (vehicleType)
[text1,text2,text3]
console.log (vehicleType[0])
t
First, you can reduce your code a bit. You can define your variable at the same time you pull the option text by using .map() instead of .each().
var arrType = $('[name="type"]> option').map(function() {
return $(this).text().trim();
}).toArray();
Second, to target the first item of the array, don't use .join() at all.
console.log(arrType[0]);
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
I have a JS function which checks if a user entered string is zero.
if (str.legth = 0) {
alert('Provide at least 1 character to create a folder.');
return;
}
But this seems to let a user pass a zero entered string.
Is there anything missing?
THanks
You've misspelled "length" and you need "==" instead of "=" to check for equailty rather than assigning a value.
If your variable is called str, then length is used to return a boolean value. You need to use either == or === for an actual comparison instead of an assignment. Additionally, a return isn't necessary.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
I'm making a function that takes a string, cuts the first half (leaving middle character if odd string.length) and adds first half to end of string.
For some reason my function only partlyworks: it adds the substr to the end but doesn't cut it from the start. I tried .replace but not working.
What am I doing wrong? And/or is there a better way?
replace returns a new string with the replacement, it doesn't modify the string you call it on.
Additionally, as Pointy pointed out, you've passed the literal string 'substr' in, rather than passing in the variable substr.
So:
s = s.replace(substr, '');
a friend just gave another way to write a function that does what I wanted mine to do . I'm an amoeba and you're all wizards
function doit(s){
split = s.length /2;
if(split % 2 !== 0) { split = split-1; }
var partOne = s.slice(0, split);
var partTwo = s.slice(split + 1, s.length);
return partTwo + partOne;
}
alert(doit('123456789qwertyuio'));
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 7 years ago.
Improve this question
I want a loop that starts with i=100, and decrements by 5 on each iteration. However, this produces an error:
for(var i=100;i>=1;i-5)
{
document.write(i+"<br />");
}
But 'i=i-5' works:
for(var i=100;i>=1;i=i-5)
{
document.write(i+"<br />");
}
Why?
The question is how we can assign a variable to a variable i=i-5.I go the question answered I taught "i=i-5" was a expression the value of i variable is i-5 and no calculation happen its just a stable variable.
The answer is that it is taking a the i value and subtracting it by 5 and not assigning.
If you just write i - 5, there is no left-hand variable, which means there is nothing which is taking the value of i and subtracting it by 5. If you write i = i - 5 then you will decrement 5 since you now have a left-hand variable.