test-coding

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.0/math.js"></script>
<input type="range" name="mySlider" id=mySlider min="0" max="1" step="0.1" value="0.5", onchange="updateUserWeight(this.value)">

<table id = "user_feature">
  <tr>
    <th>User</th>
    <th>Feature One</th>
    <th>Feature Two</th>
  </tr>
  <tr>
    <td>Anna</td>
    <td>0.3</td>
    <td>0.4</td>
  </tr>
  <tr>
    <td>Jonny</td>
    <td>0.7</td>
    <td>0.1</td>
  </tr>
</table> 

<input type="range" name="mySlider2" id=mySlider2 min="0" max="5" step="0.5" value="2.5", onchange="updateItemWeight(this.value)">

<table id = "item_feature">
  <tr>
    <th>Item</th>
    <th>Feature One</th>
    <th>Feature Two</th>
  </tr>
  <tr>
    <td>Movie 1</td>
    <td>4.5</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Movie 2</td>
    <td>3</td>
    <td>2</td>
  </tr>
</table> 

<button type="button" onclick="calculateMatrixFact()">Calculate MF!</button>

<table id = "matrix_factorization">
  <tr>
    <th>User</th>
    <th>Movie One</th>
    <th>Movie Two</th>
  </tr>
</table>

JavaScript

function updateUserWeight(slideAmount) {
    //  to retrieve the value of the slider and display it
    //	var sliderDiv = document.getElementById("sliderAmount");
    //  sliderDiv.innerHTML = slideAmount;

    var cell_change = document.getElementById("user_feature").rows[1].cells;
    cell_change[1].innerHTML = slideAmount;
    CacheValues(); // to save the new array
}

function updateItemWeight(slideAmount2) {
    //  to retrieve the value of the slider and display it
    //  var sliderDiv2 = document.getElementById("sliderAmount2");
    //  sliderDiv2.innerHTML = slideAmount2;

    var cell_change2 = document.getElementById("item_feature").rows[2].cells;
    cell_change2[2].innerHTML = slideAmount2;
    CacheValues();
}

function createGroups(arr, numGroups) {
    const perGroup = Math.ceil(arr.length / numGroups);
    return new Array(numGroups)
        .fill('')
        .map((_, i) => arr.slice(i * perGroup, (i + 1) * perGroup));
}

function calculateMatrixFact(){
	var cache = CacheValues();
  
  // split the array in two single arrays one per each user and movie
	var user_matrix = createGroups(cache.mu, 2);
	var score_matrix = createGroups(cache.ms, 2);

	// remove the string user_name and movie_name
	for (let i = 0; i < user_matrix.length && i < score_matrix.length; i++) {
  	user_matrix[i].shift();
   	score_matrix[i].shift();
	}

	var dot_matrix = [];

	// perform the dot product
	for (let j = 0; j < user_matrix.length; j++) {
    for (let k = 0; k < score_matrix.length; k++) {
        //console.log(user_matrix[j])
        //console.log(score_matrix[k])
        var dot_product = math.multiply(user_matrix[j], score_matrix[k]);
        dot_matrix.push(dot_product);
    }
	}

	// create the matrix and push back the string (first column of the table)
	var dot_prod_matrix = createGroups(dot_matrix, 2);
	dot_prod_matrix[0].unshift("Anna");
	dot_prod_matrix[1].unshift("Jonny");

	// from array to HTML table
	fetch =...