Home > Enterprise >  variable is not reassigned when changed in for loop js
variable is not reassigned when changed in for loop js

Time:09-17

when trying to change the variable value its not changing although it is global:

            let avg = 0
            let total = 0
            let courseNum = prompt("Enter the number of courses you have taken: ");
            let CourseInt = parseInt(courseNum);
            for (let index = 1; index <= CourseInt; index  ) {
                const courses = prompt("Enter the grade for course #"   index   ':');
                for (let i = 0; i < courses.length; i  ) {
                    if (parseInt(courses[i]) > 50) {
                        total =  courses[i];
                    }
                    avg = total / courses.length;
                } 
            }
            prompt("YOUR AVG: "   avg)
        </script>

in the prompt popup it shows that the avg is 0.

CodePudding user response:

here :

let courseNum = prompt("Enter the number of courses you have taken: ");
let CourseInt = parseInt(courseNum);

Instead of changing the input value to int and store it in different variable, You can simply take the inputs as number :

let courseNum = parseInt(prompt("Enter the number of courses you have taken: "));

Solution

You need to store all the courses input in one array so that we can get all the numbers.And form that array we can get the total counts.

let avg = 0
let total = 0
// we can store our inout numbers in arrayGrade array
let arrayGrade = [];
let courseNum = parseInt(prompt("Enter the number of courses you have taken: "));

for (let index = 1; index <= courseNum; index  ) {
    const courses = parseInt(prompt("Enter the grade for course #"   index   ':'));
    arrayGrade.push(courses); //appending all the courses input in arrayGrade   
    } 
    // console.log(arrayGrade)
    //get sum of all element in arrayGrade. You can console.log total and arrayGrade[i]
    for(let i in arrayGrade){
        total  = arrayGrade[i];
        avg = total / arrayGrade.length
    }
prompt('avg is : '  avg   "\n"   'total grade is : '   total)

CodePudding user response:

set an empty array let total = []; then prompting a user input for number of courses courseNum

then getting the grades inside the for loop & those grades are stored in the array total with total.push(parseInt(courses)); meaning push the number that the user has inputted and store it in the array. after the user inputs all his grade like for e.g he has done three courses so the grade prompt will loop three times & in the end your array will look something like this"[66,77,45] these are all the grade in one array.now i adding these three grades and dividing by the number of courses. hope i could explain it.you can ask me anything in the comments...

let avg = 0;
let sum = 0;
let total = [];
let courseNum = prompt("Enter the number of courses you have taken: ");
for (let i = 1; i <= courseNum; i  ) {
  const courses = prompt("Enter the grade for course #"   i);
  total.push(parseInt(courses));
}
for (let j = 0; j < total.length; j  ) {
  sum  = total[j];
}
avg = sum / total.length
alert("YOUR AVG: "   avg)

  • Related