[英]Extracting middle of string - JavaScript
I am trying to write an algorithm for this in JavaScript but I am getting a str.length is not a function...我试图在 JavaScript 中为此编写一个算法,但我得到的 str.length 不是 function...
function extractMiddle(str) {
var position;
var length;
if(str.length() % 2 == 1) {
position = str.length() / 2;
length = 1;
} else {
position = str.length() / 2 - 1;
length = 2;
}
result = str.substring(position, position + length)
}
extractMiddle("handbananna");
Because string length is not a function, it's a property.因为字符串长度不是一个函数,它是一个属性。
function extractMiddle(str) {
var position;
var length;
if(str.length % 2 == 1) {
position = str.length / 2;
length = 1;
} else {
position = str.length / 2 - 1;
length = 2;
}
return str.substring(position, position + length)
}
console.log(extractMiddle("handbananna"));
Here is an another way to do this:这是执行此操作的另一种方法:
function extractMiddle(str) {
return str.substr(Math.ceil(str.length / 2 - 1), str.length % 2 === 0 ? 2 : 1);
}
// the most amazing const getMiddle = s => s.substr(s.length - 1 >>> 1, (~s.length & 1) + 1); // should return "dd" console.log(getMiddle('middle'))
// >>> is an unsigned right shift bitwise operator. // >>> 是无符号右移按位运算符。 It's equivalent to division by 2, with truncation, as long as the length of the string does not exceed the size of an integer in Javascript.
它相当于除以 2,截断,只要字符串的长度不超过 Javascript 中整数的大小。
// About the ~ operator, let's rather start with the expression n & 1. This will tell you whether an integer n is odd (it's similar to a logical and, but comparing all of the bits of two numbers). // 关于 ~ 运算符,让我们从表达式 n & 1 开始。这将告诉您整数 n 是否为奇数(它类似于逻辑与,但比较两个数字的所有位)。 The expression returns 1 if an integer is odd.
如果整数为奇数,则表达式返回 1。 It returns 0 if an integer is even.
如果整数是偶数,则返回 0。
// If n & 1 is even, the expression returns 0. // 如果 n & 1 是偶数,则表达式返回 0。
// If n & 1 is odd, the expression returns 1. // 如果 n & 1 是奇数,则表达式返回 1。
// ~n & 1 inverts those two results, providing 0 if the length of the string is odd, and 1 if the length of the sting is even. // ~n & 1 反转这两个结果,如果字符串的长度是奇数,则提供 0,如果字符串的长度是偶数,则提供 1。 The ~ operator inverts all of the bits in an integer, so 0 would become -1, 1 would become -2, and so on (the leading bit is always the sign).
~ 运算符将整数中的所有位取反,因此 0 将变为 -1,1 将变为 -2,依此类推(前导位始终是符号)。
// Then you add one, and you get 0+1 (1) characters if the length of the string is odd, or 1+1 (2) characters if the length of the string is even. // 然后加一,如果字符串的长度是奇数,则得到 0+1 (1) 个字符,如果字符串的长度是偶数,则得到 1+1 (2) 个字符。
@author by jacobb @作者 雅各布
the link of the source is: https://codepen.io/jacobwarduk/pen/yJpAmK源码链接是: https : //codepen.io/jacobwarduk/pen/yJpAmK
That seemed to fix it!这似乎解决了它!
function extractMiddle(str) {
var position;
var length;
if(str.length % 2 == 1) {
position = str.length / 2;
length = 1;
} else {
position = str.length / 2 - 1;
length = 2;
}
result = str.substring(position, position + length)
console.log(result);
} }
https://jsfiddle.net/sd4z711y/ https://jsfiddle.net/sd4z711y/
The first 'if' statement is to get the odd number while the 'else if' is to get the even number.第一个'if'语句是得到奇数,而'else if'是得到偶数。
function getMiddle(s) { if (s.length % 2 == 1) { return s.substring((s.length / 2)+1, (s.length / 2)) } else if (s.length % 2 == 0) { return s.substring((s.length / 2)-1, (s.length / 2)+1) } } console.log(getMiddle("handers")); console.log(getMiddle("test"));
Here is my solution :-这是我的解决方案:-
![]()
function pri(word) { if (!word) return 'word should have atleast one character'; let w = [...word].reduce((acc, val) => (val == ' ' ? acc : (acc += val))); let res = ''; let length = word.length; let avg = length \/ 2; let temp = avg % 2; if (temp == 0) { res += word.charAt(avg - 1) + word.charAt(avg); } else { res += word.charAt(avg); } return res; } console.log(pri("Lime")); \/\/ even letter console.log(pri("Apple")); \/\/ odd letter console.log(pri("Apple is Fruit")); \/\/ String sequence with space console.log(pri("")); \/\/ empty string<\/code><\/pre>
"
here is my solution这是我的解决方案
function getMiddle(s){
let middle = Math.floor(s.length/2);
return s.length % 2 === 0
? s.slice(middle-1, middle+1)
: s.slice(middle, middle+1);
}
function extractMiddle(s) {
return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1);
}
extractMiddle("handbananna");
str.length is a property. str.length 是一个属性。 Just get rid of the parentheses.
只需去掉括号即可。 Example:
示例:
if (str.length == 44) {
length
is a property of string, not a function. length
是字符串的属性,而不是函数。 Do this instead:改为这样做:
str.length % 2 === 1
Also, use I suggest favoring ===
over ==
另外,使用我建议偏爱
===
不是==
由于 length 不是函数,因此无需使用 ()。
function getMiddle(str) {
if(str.length % 2 === 0 ) {
return str.substr(str.length/2-1, 2);
} else {
return str.charAt(Math.floor(str.length/2));
}
}
console.log(getMiddle("middbkbcdle"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.