Militsa

09. Set Values to Indexes in Array

Dec 25th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function indexArr(arr) {
  2.  
  3.     let count = Number(arr[0]);
  4.     let newArray = [];
  5.  
  6.     for(let i = 1; i < arr.length; i++) {
  7.         let temp = arr[i].split(' - ');
  8.         let index = temp[0];
  9.         let value = temp[1];
  10.         newArray[index] = value;
  11.     }
  12.  
  13.     for(let j = 0; j < count; j++) {
  14.         if(newArray[j] == undefined) {
  15.             console.log(0);
  16.         }
  17.  
  18.         else {
  19.             console.log(newArray[j]);
  20.         }
  21.     }
  22. }
  23.  
  24. //indexArr(['5','0 - 3','3 - -1','4 - 2'])
Add Comment
Please, Sign In to add comment