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!
stringisn't a Stringfooddeclared? How are you callingbulk?