Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exerccise 1. print squared star frame with array
- var squaredText = ["*November", "*is","*the", "*best","*month", "*of", "*the", "*year"];
- var numSpaces = 0;
- function printBoardArray(squaredText) {
- //printing the top star frame, the very first line.
- console.log("**********");
- for (var i=0; i<squaredText.length; i++)
- {
- numSpaces = 9 - squaredText[i].length;
- console.log(squaredText[i] + " ".repeat(numSpaces) + "*");
- }
- console.log("**********");
- }
- //printBoardArray(squaredText);
- //Exercise 2. Print zeroes in row and column of zero
- var matrix[
- [1,2,3],
- [4,0,5],
- [7,8,9]
- ];
- var zeroMatrixArr[];
- var zeroRow, zeroColumn = 0;
- function showZeroMatrix(matrix){
- //cloning the output array
- zeroMatrixArr = matrix.slice(0);
- //we need to find the address where the zeroes are stored, and save the address in two variables
- for(var i=0;i<matrix.length;i++)
- {
- for(var j=0; j<matrix[i].length;j++){
- //iterate to find the zero in the array
- if (matrix[i][j] === 0){
- zeroColumn = i;
- zeroRow = j;
- }
- }
- }
- //now that we have the address, we can start filling the ZEROES in the output array
- for(var i=0;i<zeroMatrixArr.length;i++)
- {
- for(var j=0; j<zeroMatrixArr[i].length;j++)
- {
- //in this for loop we filled out the rows, on the first line,
- }
- }
- }
- showZeroMatrix(matrix);
- //exercise 3. convert decimal to roman
- function toRoman(num) {
- var result = '';
- var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
- var roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
- // vamos a iterar por todo el arreglo hasta encontrar el maximo elemento divisible
- //este es el principio del abaco
- for (var i = 0;i<=decimal.length;i++) {
- // primero iteramos hasta que el modulo (residuo) de nuestro input sea menor al elemento del array por el que lo estamos dividiendo
- while (num%decimal[i] < num) {
- //aqui se agrega a la cadena el caracter
- result += roman[i];
- //aqi se actualiza el nuevo valor del numero para el modulo pra la siguiente iteracion
- num -= decimal[i];
- }
- }
- return result;
- }()
- //exercise 5. compute first 100 fibonacci sequence numbers
- var num = 100;
- function fibonacci(num) {
- if (num === 0){
- return 0;
- }
- else if (num === 1) {
- return 1;
- }
- else {
- console.log(fibonacci(num-1) + fibonacci(num-2));
- }
- return;
- }
- //fibonacci(num);
- //Exercise 6. convert decimal to binary // if greater than 32 bits, call it waay too big
- function decToBinary(num){
- var result = [];
- for (var i=num; i>0; i=parseInt(i/2)) {
- result.push(i%2);
- }
- if (result.length() > 32 ) {
- console.log("Error: " +num+ "is waay too big for 32 bits");
- }
- else
- {
- console.log("Your binary vaue is: ");
- console.log(result.reverse().join(""));
- }
- }
- function transpose(arr){
- var n=arr.length;
- for (var i=0; i<n/2; i++) {
- for (var j=i; j<n-i-1; j++) {
- var tmp=arr[i][j];
- arr[i][j]=arr[j][n-i-1];
- arr[j][n-i-1]=arr[n-i-1][n-j-1];
- arr[n-i-1][n-j-1]=arr[n-j-1][i];
- arr[n-j-1][i]=tmp;
- }
- }
- return arr;
- }()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement