Advertisement
libdo

Untitled

Oct 17th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Exerccise 1. print squared star frame with array
  2.  
  3. var squaredText = ["*November", "*is","*the", "*best","*month", "*of", "*the", "*year"];
  4. var numSpaces = 0;
  5.  
  6. function printBoardArray(squaredText) {
  7.  
  8. //printing the top star frame, the very first line.
  9. console.log("**********");
  10. for (var i=0; i<squaredText.length; i++)
  11. {
  12. numSpaces = 9 - squaredText[i].length;
  13. console.log(squaredText[i] + " ".repeat(numSpaces) + "*");
  14. }
  15. console.log("**********");
  16. }
  17. //printBoardArray(squaredText);
  18.  
  19. //Exercise 2. Print zeroes in row and column of zero
  20. var matrix[
  21. [1,2,3],
  22. [4,0,5],
  23. [7,8,9]
  24. ];
  25. var zeroMatrixArr[];
  26. var zeroRow, zeroColumn = 0;
  27.  
  28. function showZeroMatrix(matrix){
  29. //cloning the output array
  30. zeroMatrixArr = matrix.slice(0);
  31. //we need to find the address where the zeroes are stored, and save the address in two variables
  32. for(var i=0;i<matrix.length;i++)
  33. {
  34. for(var j=0; j<matrix[i].length;j++){
  35. //iterate to find the zero in the array
  36. if (matrix[i][j] === 0){
  37. zeroColumn = i;
  38. zeroRow = j;
  39. }
  40. }
  41. }
  42. //now that we have the address, we can start filling the ZEROES in the output array
  43. for(var i=0;i<zeroMatrixArr.length;i++)
  44. {
  45. for(var j=0; j<zeroMatrixArr[i].length;j++)
  46. {
  47. //in this for loop we filled out the rows, on the first line,
  48. }
  49. }
  50. }
  51.  
  52. showZeroMatrix(matrix);
  53.  
  54.  
  55. //exercise 3. convert decimal to roman
  56. function toRoman(num) {
  57. var result = '';
  58. var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  59. var roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
  60.  
  61. // vamos a iterar por todo el arreglo hasta encontrar el maximo elemento divisible
  62. //este es el principio del abaco
  63. for (var i = 0;i<=decimal.length;i++) {
  64. // primero iteramos hasta que el modulo (residuo) de nuestro input sea menor al elemento del array por el que lo estamos dividiendo
  65. while (num%decimal[i] < num) {
  66. //aqui se agrega a la cadena el caracter
  67. result += roman[i];
  68. //aqi se actualiza el nuevo valor del numero para el modulo pra la siguiente iteracion
  69. num -= decimal[i];
  70.  
  71. }
  72. }
  73. return result;
  74. }()
  75.  
  76. //exercise 5. compute first 100 fibonacci sequence numbers
  77. var num = 100;
  78.  
  79. function fibonacci(num) {
  80. if (num === 0){
  81. return 0;
  82. }
  83. else if (num === 1) {
  84. return 1;
  85. }
  86.  
  87. else {
  88. console.log(fibonacci(num-1) + fibonacci(num-2));
  89. }
  90. return;
  91. }
  92.  
  93. //fibonacci(num);
  94.  
  95. //Exercise 6. convert decimal to binary // if greater than 32 bits, call it waay too big
  96.  
  97. function decToBinary(num){
  98. var result = [];
  99. for (var i=num; i>0; i=parseInt(i/2)) {
  100. result.push(i%2);
  101. }
  102. if (result.length() > 32 ) {
  103. console.log("Error: " +num+ "is waay too big for 32 bits");
  104. }
  105. else
  106. {
  107. console.log("Your binary vaue is: ");
  108. console.log(result.reverse().join(""));
  109. }
  110. }
  111.  
  112. function transpose(arr){
  113. var n=arr.length;
  114. for (var i=0; i<n/2; i++) {
  115. for (var j=i; j<n-i-1; j++) {
  116. var tmp=arr[i][j];
  117. arr[i][j]=arr[j][n-i-1];
  118. arr[j][n-i-1]=arr[n-i-1][n-j-1];
  119. arr[n-i-1][n-j-1]=arr[n-j-1][i];
  120. arr[n-j-1][i]=tmp;
  121. }
  122. }
  123. return arr;
  124. }()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement