Advertisement
shady_obeyd

09.Set Values To Indexes in an Array

Nov 16th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setValuesToIndexes(input, arr) {
  2.  
  3.     let n = Number(input[0]);
  4.  
  5.     for(let i = 0; i < n; i++){
  6.         arr.push(0);
  7.     }
  8.  
  9.     for(let i = 1; i < input.length; i++){
  10.         let currentCommand = input[i].split(' - ');
  11.  
  12.         let index = Number(currentCommand[0]);
  13.         let value = Number(currentCommand[1]);
  14.  
  15.         arr[index] = value;
  16.     }
  17.  
  18.     for(let i = 0; i < arr.length; i++){
  19.         console.log(arr[i]);
  20.     }
  21.  
  22. }
  23.  
  24. let arr = [];
  25. let input = ['5', '0 - 3', '3 - -1', '4 - 2'];
  26. setValuesToIndexes(input, arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement