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