Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

sorting - How to sort and compare these arrays on codewars?

Currently, I was working on solving kata on codewars, and I solved one and it runs good in the browser, but on codewars when I am trying to attempt a solution it gives me an error.
I need your help, I don't understand why, but this code doesn't work here, but perfectly works in a browser.
I am getting an error :

TypeError: Cannot read property '1' of undefined
at cakes
at runTest
at /home/codewarrior/index.js:52:5
at begin
at it
at /home/codewarrior/index.js:51:3
at /runner/frameworks/javascript/cw-2.js:152:11
at Promise._execute
at Promise._resolveFromExecutor
at new Promise
at describe
at /home/codewarrior/index.js:45:5
at /home/codewarrior/index.js:112:5
at Object.handleError

Why it's like that? Any thougths?

function cakes(recipe, available) {
let avaiableArr = Object.entries(available)
let recipeArr = Object.entries(recipe)
// console.log(avaiableArr)
// console.log(recipeArr)
if (avaiableArr.length >= recipeArr.length) {
const sorted = (array) => array.sort(function (a, b) {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
return 0;
})
sorted(recipeArr)
sorted(avaiableArr)
console.log(avaiableArr)
console.log(recipeArr)
let portions = avaiableArr.map((element, index) => {
return element[1] / recipeArr[index][1]
});
if (portions.some(el => el < 1)) {
console.log(0)
return 0;
}
console.log(Math.floor(Math.min(...portions)))
return Math.floor(Math.min(...portions))
}
else {
console.log(0)
return 0
}
}
question from:https://stackoverflow.com/questions/65902378/how-to-sort-and-compare-these-arrays-on-codewars

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...