javascript - How can I rewrite the .length() property using slice()? - Stack Overflow

This is my assignment:By now you should have worked with the length property of strings, e.g. "hel

This is my assignment:

By now you should have worked with the length property of strings, e.g. "hello".length. Your task is to write a function called stringLength that accepts a string as a parameter and putes the length of that string; however, as you may have guessed, you are not allowed to use the length property of the string!

Instead, you'll need to make use of the string method called slice. For our purposes, we can consider slice as taking one argument -- the index to begin slicing from, and returns a new string starting from that index onwards.

This is what I tried:

function stringLength(string){
  var count = count++;
  if(string.slice(0)){
     return count}
 return  stringLength(string.slice(0,-1)) 
 }
console.log(stringLength("game"))

I am trying to slice each character of the string back to start index, index 0, and then accumulate my count variable. I do not understand why my count variable is not accumulating.

This is my assignment:

By now you should have worked with the length property of strings, e.g. "hello".length. Your task is to write a function called stringLength that accepts a string as a parameter and putes the length of that string; however, as you may have guessed, you are not allowed to use the length property of the string!

Instead, you'll need to make use of the string method called slice. For our purposes, we can consider slice as taking one argument -- the index to begin slicing from, and returns a new string starting from that index onwards.

This is what I tried:

function stringLength(string){
  var count = count++;
  if(string.slice(0)){
     return count}
 return  stringLength(string.slice(0,-1)) 
 }
console.log(stringLength("game"))

I am trying to slice each character of the string back to start index, index 0, and then accumulate my count variable. I do not understand why my count variable is not accumulating.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 26, 2016 at 22:07 heliuheliu 671 silver badge10 bronze badges 1
  • That's different definition of slice than what javascript uses. – Ouroborus Commented Nov 26, 2016 at 22:27
Add a ment  | 

6 Answers 6

Reset to default 5

An iterative proposal.

function stringLength(string) {
    var count = 0;
    while (string) {
        string = string.slice(1);
        count++;
    }
    return count;
}

console.log(stringLength("game"));

A recursive proposal.

function stringLength(string) {
    return string ? 1 + stringLength(string.slice(1)) : 0;
}

console.log(stringLength("game"));

Hmm i tried to write code in the same format that you did.

function stringLength(str, count){
  if(!str.slice(0)){
     return count;
  }
    return  stringLength(str.slice(0,-1), ++count) 
 }
console.log(stringLength("game", 0))

I'll point out the mistakes in your original code so that its easy to understand.

  1. The recursion base case was incorrect. string.slice(0) will return true if the string is non-empty, so use !string.slice(0)

  2. The count value was not initialized and it wasn't being passed down the recursion.

Your count variable is a separate variable for each function invocation, so it will always get the same value and not keep incrementing.

You could use this:

function stringLength(string){
    return string ? 1 + stringLength(string.slice(0,-1)) : 0; 
}
console.log(stringLength("game"))

A bit shorter would be to take out the first character instead of the last:

    return string ? 1 + stringLength(string.slice(1)) : 0; 

You really should try to figure it out yourself. Otherwise, are you really learning the subject?

function stringLength(string) {
  if(!string) return 0;
  var length = -1;
  while(string.slice(length) !== string) --length;
  return -length;
}

A variation taking into account your odd definition of slice():

function stringLength(string) {
  var length = 0;
  while(string.slice(length) !== "") ++length;
  return length;
}

I guess you could try to use recursion like this:

function stringLength(string) {
    if (string) {
        return 1 + stringLength(string.slice(1))
    } else return 0
}

function stringLength(string) {
    var len = 0;
    while (string) {
        string = string.substring(1);
        len++;
    }
    return len;
}

console.log(stringLength("boss"));

this works as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744723314a4590039.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信