Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  143] [ 5]  / answers: 1 / hits: 37841  / 8 Years ago, wed, august 17, 2016, 12:00:00

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

More From » string

 Answers
17

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

[#61007] Monday, August 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ciarajourneyv

Total Points: 428
Total Questions: 95
Total Answers: 90

Location: Maldives
Member since Sat, Feb 11, 2023
1 Year ago
;