admin管理员组

文章数量:1182734

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");

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");
Share Improve this question asked Aug 17, 2016 at 16:01 MadCatm2MadCatm2 1,0015 gold badges26 silver badges41 bronze badges 4
  • 3 Its str.length not str.length() – Jonathan Newton Commented Aug 17, 2016 at 16:05
  • String length is a property not a method. so use str.length – twothreebrent Commented Aug 17, 2016 at 16:05
  • Ya property, not function. – Juan Tomas Commented Aug 17, 2016 at 16:05
  • 1 Oh...I wen't into Java mode there for a sec – MadCatm2 Commented Aug 17, 2016 at 16:08
Add a comment  | 

12 Answers 12

Reset to default 15

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

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

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

Since length is not a function, there is no need to use ().

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"));

本文标签: Extracting middle of stringJavaScriptStack Overflow