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 callingbulk
? – 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
2 Answers
Reset to default 7If 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
版权声明:本文标题:javascript - TypeError: string.split is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743600541a2508517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论