so I have a form which has 4 blocks of results at the end, the results are based on answers selected by the user. What I want to do is get the values of all the blocks individually( I have tried doing this via the DOM but I observed the value attribute is blank for some reason) and compare all of them to find the largest value, which I will use for some logic that I want to implement on the results page.
I have tried using the code below to achieve that, however there is some iteration going on and I am not sure why.I have declared an array for the values and logged it to the console for clarity.
I am still not proficient in JavaScript, so please pardon me.Please kindly guide me in the redirection.
// get values of the CAT scores from the DOM & compare to get largest value
$('#field_wyepk, #field_wyepk2, #field_wyepk3 , #field_wyepk32').change(function(){
var commerceScore = $("#field_wyepk").val();
var scitechScore = $("#field_wyepk2").val();
var humanitiesScore = $("#field_wyepk3").val();
var eduScore = $("#field_wyepk32").val();
console.log(commerceScore);
console.log(scitechScore);
console.log(humanitiesScore);
console.log(eduScore);
var array = [commerceScore,scitechScore, humanitiesScore,eduScore];
console.log(array);
var largest= 0;
for (i=0; i<=largest; i++){
if (array[i]>largest && array[i] !== ""){
var largest=array[i];
}
}
});
Please see attached snippet of the console results
All I need is to get an array with [71, 85, 76, 56] as an example using the snippet and I will do the rest.
As was said in the comment, you have a change listener listening for changes to all of the results. Any time one of the results is changed, all of them are added to your array and logged to the console. Maybe a change listener isn't what you need - perhaps you could get the user to click a button when all the results have been entered, then on the button click create the array. Otherwise, you could parse your array and check that all the values have a length?
const validArray = !array.some(item => item.length === 0);
(will return false if your array contains one or more instances of empty string, or true if your array is valid. Only proceed to the next step if the value is true)
Also note that your array is made up of strings, not integers. I'm not sure if this is what you want.