-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsPractice.js
163 lines (114 loc) · 3.54 KB
/
jsPractice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// for (var i = 1; i <= 30; i++){
// if (i % 7 === 0) {
// continue;
// }
// console.log(i)
// }
// for (let index = 1; index < 50; index++) {
// if(index % 10 == 0 && index % 15 == 0){
// console.log("donkey")
// } else if (index % 2 != 0) {
// console.log("Monkey")
// continue;
// } else{
// console.log(index)
// }
// }
//------------ forEach
//Crea una matriz con 6 de tus comidas favoritas.
//Con el ciclo que elijas, recorre el conjunto, pero solo imprime los alimentos con un índice par.
// let food = ["chilaquiles", "tortas", "tacos", "cochinita", "pollito", "alitas"];
// for (let index = 0; index < food.length; index++) {
// if( index % 2 === 0){
// console.log(food[index]);
// }
// }
//suma de elementos del array, la suma final debe ser 29
// var sum = 0;
// var numbers = [2, 3, 6, 1, 7, 10];
// for(var i = 0; i <= numbers.length; i++){
// sum += numbers[i];
// console.log(sum);
// }
// // Log the sum
// console.log(sum);
// var currentLargest = 0;
// var numbers = [10, 16, 99, 0, 52, 41, 7];
// for (var i = 0; i <= numbers.length; i++){
// if( numbers[i] > currentLargest)
// {
// currentLargest = numbers[i]
// }
// }
// console.log("The largest number is: " + currentLargest);
//---------- OBJETOS
// var olympicRecords =
// {
// athletics100Men: "Usain Bolt",
// athleticsLongJumpMen: "Mike Powel",
// swimming200Men: "Michael Phelps",
// swimming400Women: "Katie Ledecky"
// }
// for (var keys in olympicRecords){
// // recordName is a **key** in the object
// console.log("keys: " + keys);
// }
//console.log(Object.keys(olympicRecords));
// for (var key in olympicRecords){
// console.log("The value of " + key + " is " + olympicRecords[key]);
// }
//------------Battleship
// var board = [
// [null, null, null, "S", null],
// [null, "S", null, "S", null ],
// ["S", null, null, null, null ],
// [null, "S", null, null, null],
// [null, null, null, null, "S"]
// ];
// var firstRow = board[0];
// console.log("First Row: " + firstRow);
// var secondRow = board[1];
// console.log("Second Row: " + secondRow);
// var thirdRow = board[2];
// console.log("Third Row: " + thirdRow);
// var firstRow = board[0];
// var emptySpace = firstRow[0];
// var ship = firstRow[3];
// console.log("Empty Space: " + emptySpace);
// console.log("Ship: " + ship);
// We can also shortcut assigning the row to a variable
// console.log("Third row, first col: " + board[2][0]);
var board = [
[null, null, null, "S", null],
[null, "S", null, "S", null ],
["S", null, null, null, null ],
[null, "S", null, null, null],
[null, null, null, null, "S"]
];
for (var i = 0; i < 10; i++){
var row = getRandomNum();
var column = getRandomNum();
var randomSquare = board[row][column];
if (randomSquare === "S"){
console.log("Hit on: " + row + ", " + column);
board[row][column] = null;
}
}
function getRandomNum(){
return Math.floor(Math.random() * 5);
}
// for (var i = 0; i < board.length; i++){
// // A single row, such as board[0], board[1], board[2]
// var row = board[i];
// // Loop over each element in the row
// // We use "j" because "i" is already being used.
// // What would happen if we used i in this loop instead? Try it.
// for (var j = 0; j < row.length; j++){
// var column = row[j];
// // If the column is a ship, log the coords
// if (column === "S"){
// console.log(`Ship found at ${ i }, ${ j }`);
// }
// // Instead of using variables, you could reference: board[i][j]
// }
// }