admin管理员组

文章数量:1352562

I'm solving an exercise problem. Solved it in Visual Studio (it worked), copied the solution to the browser (Codewars challenge) and it returned this error:

TypeError: string.split is not a function
   at bulk
    at _
    at begin
    at it
        at /runner/frameworks/javascript/cw-2.js:159:11
    at Promise._execute
    at Promise._resolveFromExecutor
    at new Promise
    at Object.describe
            at Object.handleError
        at ContextifyScript.Script.runInThisContext
    at Object.exports.runInThisContext

Here's my code:

function bulk(string) {
    var arr = string.split(", ");
    var whatYouAte = [];
    for (var i = 0; i < arr.length; i++) {
        arr[i] = arr[i].replace(/g /g, ' ');
        whatYouAte.push(arr[i].split(" "));
    }

    var proteins = 0; 
    var calories = 0; 

    console.log(whatYouAte);

    for (var j = 0; j < whatYouAte.length; j++) {
        var foodAmount = whatYouAte[j][0];
        var foodName = whatYouAte[j][1];
        var foodProteinKcal = food[foodName][0];
        var foodCarbKcal = food[foodName][1];
        var foodFatKcal = food[foodName][2];
        proteins += foodAmount / 100 * foodProteinKcal;
        calories += foodAmount / 100 * (foodProteinKcal + foodCarbKcal + foodFatKcal); 

    }

    return "Total proteins: " + proteins + " grams, Total calories: " + calories + ".";
}

I'm solving an exercise problem. Solved it in Visual Studio (it worked), copied the solution to the browser (Codewars challenge) and it returned this error:

TypeError: string.split is not a function
   at bulk
    at _
    at begin
    at it
        at /runner/frameworks/javascript/cw-2.js:159:11
    at Promise._execute
    at Promise._resolveFromExecutor
    at new Promise
    at Object.describe
            at Object.handleError
        at ContextifyScript.Script.runInThisContext
    at Object.exports.runInThisContext

Here's my code:

function bulk(string) {
    var arr = string.split(", ");
    var whatYouAte = [];
    for (var i = 0; i < arr.length; i++) {
        arr[i] = arr[i].replace(/g /g, ' ');
        whatYouAte.push(arr[i].split(" "));
    }

    var proteins = 0; 
    var calories = 0; 

    console.log(whatYouAte);

    for (var j = 0; j < whatYouAte.length; j++) {
        var foodAmount = whatYouAte[j][0];
        var foodName = whatYouAte[j][1];
        var foodProteinKcal = food[foodName][0];
        var foodCarbKcal = food[foodName][1];
        var foodFatKcal = food[foodName][2];
        proteins += foodAmount / 100 * foodProteinKcal;
        calories += foodAmount / 100 * (foodProteinKcal + foodCarbKcal + foodFatKcal); 

    }

    return "Total proteins: " + proteins + " grams, Total calories: " + calories + ".";
}

I think I once had a similar problem and solved it by making this.split(), but now this doesn't work (Codewars doesn't accept, returns "TypeError: this.split is not a function"). Thanks!

Share Improve this question asked May 27, 2017 at 16:02 P. PrunesquallorP. Prunesquallor 5712 gold badges11 silver badges27 bronze badges 3
  • 1 Most probable cause is that your variable string isn't a String – Lixus Commented May 27, 2017 at 16:05
  • Where is food declared? How are you calling bulk? – Scott Marcus Commented May 27, 2017 at 16:05
  • @Scott: Food is an object declared within Codewars and used when testing the solution; I can't see it. – P. Prunesquallor Commented May 27, 2017 at 16:27
Add a ment  | 

2 Answers 2

Reset to default 7

If you are getting a number you can use the .toString() method.

I'm using something like this:

if (typeof string === 'number') {
    string = num.toString();
}

First, use the typeof operator to check if it's a number. Then use the .toString method to change it to a string. Hope it helps :)

If you call .split() on something other than a string, the JS runtime won't find the .split() method for that type. Make sure you are passing a string to your function.

Here's an example:

var result = "Scott Marcus".split(" "); 

console.log(result); // ["Scott", "Marcus"]

var value = 42
value.split("4");  // ERROR: split is not a function

本文标签: javascriptTypeError stringsplit is not a functionStack Overflow