1

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!

3
  • 1
    Most probable cause is that your variable string isn't a String Commented May 27, 2017 at 16:05
  • Where is food declared? How are you calling bulk? 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. Commented May 27, 2017 at 16:27

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to StackOverflow! Great first answer. :)
4

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

3 Comments

Thank you for your comment. My bad, I realized just now that it is indeed an array, e.g. ['300g turkey, 300g potatoes, 100g cucumber']. So I changed the first two lines of code to read: function bulk(arr) { var arr = arr[0].split(", "); (etc) but this again gives an error: TypeError: Cannot read property 'split' of undefined at bulk
@PerryPrunesquallor Now the error indicates that arr[0] is undefined, not that split is not a function. This indicates that your input arr is still not what you think it is. This error indicates that the function is being called without any input at all.
@ScottMarkus, that's true, I mistakenly tested for a string in VB instead of an array and that's why it worked in VB and not on Codewars... I'm blind, what can I say

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.