简体   繁体   中英

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

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.

// 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). The expression returns 1 if an integer is odd. It returns 0 if an integer is even.

// If n & 1 is even, the expression returns 0.

// If n & 1 is odd, the expression returns 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. 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).

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

@author by jacobb

the link of the source is: 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/

The first 'if' statement is to get the odd number while the 'else if' is to get the even number.

 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 :-

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. Just get rid of the parentheses. Example:

if (str.length == 44) {

length is a property of string, not a function. 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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM