I'm attempting to write some code that runs over an array of lessons within the object below and return the percentage of completed lessons (ones marked as completed: true).
When I console.log the outcome it just shows as [Function completion], rather than a number as expected. Where am I going wrong?
For reference I'm using JS with React Native, but this should only concern JS if I'm correct?
WorkoutCategoryData.js
export default [
{
title: "Title 1",
subtitle: "Subtitle 1",
lessonCount: 4,
lessons: [
{
title: "Lesson Name 1",
completed: true
},
{
title: "Lesson Name 2",
completed: true
},
{
title: "Lesson Name 3",
completed: false
},
{
title: "Lesson Name 4",
completed: false
},
],
completion: function() {
this.findCompletion();
}
},
{
title: "Title 2",
subtitle: "Subtitle 2",
lessonCount: 3,
lessons: [
{
title: "Lesson Name 5",
completed: true
},
{
title: "Lesson Name 6",
completed: true
},
{
title: "Lesson Name 7",
completed: true
}
],
completion: function() {
this.findCompletion();
}
}
];
function findCompletion(lessons) {
let lessonCompletion = 1;
let completedLessons = lessons.filter(lesson => lesson.completed === true)
.length;
let totalLessons = lessons.length;
lessonCompletion = (completedLessons / totalLessons) * 100;
return lessonCompletion;
}
Workouts.js
import WorkoutCategoryData from "./data/WorkoutCategoryData";
{WorkoutCategoryData.map(cat => {
let currentCompletion = cat.completion;
console.log(currentCompletion);
return (
<View style={styles.workoutContainer} key={cat.title}>
<View style={styles.workoutProgress}>
<AnimatedCircularProgress
size={50}
width={5}
rotation={0}
fill={cat.completion}
tintColor={Colors.primary}
backgroundColor="#dddddd"
/>
</View>
<View style={styles.workoutText}>
<Text style={styles.workoutTitle}>{cat.title}</Text>
{cat.subtitle ? (
<Text style={styles.workoutSubtitle}>{cat.subtitle}</Text>
) : null}
</View>
</View>
);
})}
I'm hoping to get an Integer number I can then pass in as a prop in a component for a progress indicator. Any help would be very much appreciated, thanks in advance!
console.log? And the call to thecompletions?