In this excercise I create 3 function. score()
to generate a random Number. generateStudents()
to create a list Object. Object has format {student:'name',marks:{...}}
as define in generateStudents()
. In last function, I create obj
and obj1
. obj
is a list (assign value equal to function 2) and obj1
has format {"subject":...,"student_score:{}}
.
I assign key:value
in a for loop to create the property "student_score" (also an object) but when i get out of for loop and print obj1
it does not include the part i have assign in for loop.
How can i assign key:value
for a property (also an object) using for loop?
var score = function getRandomMark(start, end, step) {
var num = start
var arr = []
arr.push(num)
while (num <= (end - step)) {
num = step
arr.push(num)
}
var index = Math.floor(Math.random() * (arr.length))
return (arr[index])
}
var generateStudents = function generateStudent(nameArr) {
var studentArr = []
for (let i = 0; i < nameArr.length; i ) {
var student = {
"name": nameArr[i],
"marks": {
"literature": score(6, 10
, 0.5),
"maths": score(6, 10
, 0.5),
"chemistry": score(6, 10
, 0.5),
"history": score(6, 10
, 0.5),
"biology": score(6, 10
, 0.5)
}
}
studentArr.push(student)
}
return studentArr
}
var markList = ["Chi", "Duc", "Huy"]
var findStudentMinMax = function findStudentMinMax() {
var obj = generateStudents(markList)
console.log(typeof (obj))
var obj1 = {}
var arr1 = []
const keys1 = Object.keys(obj[0]["marks"])
for (let i = 0; i < keys1.length; i ) {
var name = obj[0]["name"]
var markObj = obj[0]["marks"]
obj1["subject"] = keys1[i]
obj1["student_score"] = {}
//get all subject
for (let i = 0; i < obj1.length; i ) {
obj1["student_score"][name] = markObj[obj1["subject"]]
}
console.log(obj1)
arr1.push(obj1)
}
}
findStudentMinMax()
CodePudding user response:
I've made a few changes to the code inside findStudentMinMax
.
- Changed
obj
tostudents
as it makes the code easier to understand. - Used
j
in the inner for loop as it was clashing with thei
being used in the outer loop. - Used
students.length
instead ofobj1.length
in the inner loop as I believe we're trying to loop through the students. - Found the student name and marks in the inner loop, as that's where we're going through each student.
var score = function getRandomMark(start, end, step) {
var num = start
var arr = []
arr.push(num)
while (num <= (end - step)) {
num = step
arr.push(num)
}
var index = Math.floor(Math.random() * (arr.length))
return (arr[index])
}
var generateStudents = function generateStudent(nameArr) {
var studentArr = []
for (let i = 0; i < nameArr.length; i ) {
var student = {
"name": nameArr[i],
"marks": {
"literature": score(6, 10
, 0.5),
"maths": score(6, 10
, 0.5),
"chemistry": score(6, 10
, 0.5),
"history": score(6, 10
, 0.5),
"biology": score(6, 10
, 0.5)
}
}
studentArr.push(student)
}
return studentArr
}
var markList = ["Chi", "Duc", "Huy"]
var findStudentMinMax = function findStudentMinMax() {
var students = generateStudents(markList) // changed name of obj to students to make things clearer
// console.log(typeof (obj))
var obj1 = {}
var arr1 = []
const keys1 = Object.keys(students[0]["marks"])
for (let i = 0; i < keys1.length; i ) {
// var name = obj[0]["name"]
// var markObj = students[0]["marks"]
obj1["subject"] = keys1[i]
obj1["student_score"] = {}
//get all subject
for (let j = 0; j < students.length; j ) { // j instead of i
let name = students[j]["name"]; // getting name from students
let markObj = students[j]["marks"] // getting markObj from students
obj1["student_score"][name] = markObj[obj1["subject"]]
}
console.log(obj1)
arr1.push(obj1)
}
}
findStudentMinMax()
CodePudding user response:
It seems that the i < obj1.length;
always returns false
. Hence your for loop never runs. You can never access .length
on an object so you have to change the for loop to i < Object.keys(obj1).length
. I am still not sure what you want your output object to be because you only stated you wanted to insert the value inside the object. Please find below the runnable code snippet.
var score = function getRandomMark(start, end, step) {
var num = start
var arr = []
arr.push(num)
while (num <= (end - step)) {
num = step
arr.push(num)
}
var index = Math.floor(Math.random() * (arr.length))
return (arr[index])
}
var generateStudents = function generateStudent(nameArr) {
var studentArr = []
for (let i = 0; i < nameArr.length; i ) {
var student = {
"name": nameArr[i],
"marks": {
"literature": score(6, 10
, 0.5),
"maths": score(6, 10
, 0.5),
"chemistry": score(6, 10
, 0.5),
"history": score(6, 10
, 0.5),
"biology": score(6, 10
, 0.5)
}
}
studentArr.push(student)
}
return studentArr
}
var markList = ["Chi", "Duc", "Huy"]
var findStudentMinMax = function findStudentMinMax() {
var obj = generateStudents(markList)
console.log(typeof (obj))
var obj1 = {}
var arr1 = []
const keys1 = Object.keys(obj[0]["marks"])
for (let i = 0; i < keys1.length; i ) {
var name = obj[0]["name"]
var markObj = obj[0]["marks"]
obj1["subject"] = keys1[i]
obj1["student_score"] = {}
//get all subject
for (let i = 0; i < Object.keys(obj1).length; i ) {
obj1["student_score"][name] = markObj[obj1["subject"]]
}
console.log(obj1)
arr1.push(obj1)
}
}
findStudentMinMax()