123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //html tree
- let obj = {
- body: {
- tagName: 'body',
- attrs: {},
- paired: true,
- subTags: [
- {
- tagName: 'div',
- attrs: {},
- paired: true,
- subTags: [
- {
- tagName: 'span',
- attrs: {},
- paired: true,
- text: 'Enter data please:',
- },
-
- {
- tagName: 'br',
- paired: false
- },
-
- {
- tagName: 'input',
- attrs: {
- type: 'text',
- id: 'name'
- },
- paired: false
- },
-
- {
- tagName: 'input',
- attrs: {
- type: 'text',
- id: 'surname'
- },
- paired: false
- }
- ]
- },
- {
- tagName: 'div',
- attrs: {},
- paired: true,
- subTags: [
- {
- tagName: 'button',
- attrs: {
- id: 'ok'
- },
- paired: true,
- text: 'Ok'
-
- },
- {
- tagName: 'button',
- attrs: {
- id: 'cancel'
- },
- paired: true,
- text: 'Cancel'
- },
- ],
- },
- ]
- }
- }
- console.log(`<button id='cancel'> innerText:`, obj.body.subTags[1].subTags[1].text)
- console.log(`<input type='text' id='surname'> id:`, obj.body.subTags[0].subTags[3].attrs.id)
- //declarative fields
- var notebook = {
- brand: prompt('Notebook brand:','') || "HP",
- type: prompt('Notebook type:','') || "440 G4",
- model: prompt('Notebook model:','') || "Y7Z75EA",
- ram: +prompt('Notebook ram:','') || 4,
- size: prompt('Notebook size:','') || "14",
- weight: +prompt('Notebook weight:','') ||1.8,
- resolution: {
- width: +prompt('Notebook width:','') || 1920,
- height: +prompt('Notebook height','') || 1080,
- },
- };
- console.log(notebook)
- var phone = {
- brand: prompt('Phone brand:','') || "meizu",
- model: prompt('Phone model:','') || "m2",
- ram: +prompt('Phone amount of ram:','') || 2,
- color: prompt('Phone color','') || "black",
- };
- console.log(phone)
- var person = {
- Name: prompt('Person name:','') || 'Donald',
- surname: prompt('Person surname:','') || "Trump",
- married: confirm('is married?'),
- }
- console.log(person)
- //object links
- notebook.owner = phone.owner = person
- person.phone = phone
- person.notebook = notebook
- console.log(phone, notebook)
- console.log(person.phone.owner.notebook.owner.phone == person.phone )
- //imperative array fill 3
- let imperatve = [prompt('input array[0] element','')||0, prompt('input array[1] element','')||1, prompt('input array[2] element','')||2]
- console.log(imperatve)
- //while confirm
- while(!confirm('break the loop?')){}
- //array fill
- let arrayFill = []
- for(let i = 0, str; i < arrayFill.length+1; i++) {
- str = prompt('input array element','')
- if(!str && str !== '') {
- break;
- } else {
- arrayFill.push(str)
- }
- }
- console.log(arrayFill)
- let arrayNoPush = []
- for(let i = 0, str2; i < arrayNoPush.length+1; i++) {
- str2 = prompt('input arrayNoPush element','')
- if(!str2 && str2 !== '') {
- break;
- } else {
- arrayNoPush[arrayNoPush.length] = str2
- }
- }
- console.log(arrayNoPush)
- //infinite probability
- let trys = 0
- while(true) {
- trys++
- let rand = Math.random()
- if(rand > 0.9) {
- alert(`Number of iterations: ${trys}`)
- break;
- }
- }
- //empty loop
- while(!prompt('Input smth and press OK to stop','')){}
- //progression sum
- let N = 10
- let step = 1
- let sum = 0
- for(let i = 1; i <= N; i++) {
- sum = sum + step
- step += 3
- }
- console.log('progression sum:', sum)
- //chess one line
- let strlen = 11
- let str = ''
- for(let i = 0; i < strlen; i++) {
- i % 2 === 0? str += ' ' : str += '#'
- }
- console.log(str, str.length)
- //numers
- let numbers = ''
- for(let i = 0; i < 10; i++) {
- for(let i = 0; i < 10; i++) {
- numbers += `${i}`
- }
- numbers += '\n'
- }
- console.log(numbers)
- //chess
- let height = 3
- let width = 12
- let chessboard = ''
- for(let i = 0; i < height; i++) {
- for(let j = 0; j < width; j++) {
- i % 2 === 0? (j % 2 === 0? chessboard+='.' : chessboard+='#') : (j % 2 === 0? chessboard +='#' : chessboard+='.')
- }
- chessboard += '\n'
- }
- console.log(chessboard)
- //cubes
- let cubes = new Array(10)
- for(let i = 0; i < cubes.length; i++) { cubes[i] = i**3 }
- console.log(cubes)
- //multiply table
- let mulTable = []
- for(let i = 0; i < 10; i++) {
- let innerArr = []
- for(let j = 0; j < 10; j++) {
- innerArr.push(i * j)
- }
- mulTable[i] = innerArr
- }
- console.log(mulTable[5][6]) //будет 30 (но есть нулевые елементы в массиве)
- //matrix to html table
- let tableStr = ''
- let colors = ['red', 'orange', 'yellow', 'green', 'skyblue', 'blue', 'purple', 'red', 'orange']
- let prevColors = []
- tableStr += '<table>'
- for (let i = 1; i < mulTable.length; i++) {
- tableStr += `<tr>`
- prevColors.push(colors[i - 1])
- for (let j = 1; j < mulTable.length; j++) {
- tableStr += `<td
- style="text-align: center;
- color: white;
- text-shadow: 1px 1px 2px black;
- background-color: ${j >= prevColors.length? prevColors[i-1] : prevColors[j-1]}";"
- > ${mulTable[i][j]} </td>`
- }
- tableStr += '</tr>'
- }
- tableStr += '</table>'
- document.write(tableStr)
- //blue belt
- let trigon = ''
- for(let i = 6, step = 1; i > 0; i--) {
- let repeats = 10
- for(let j = 0; j <= repeats; j++) {
- if(j === i - 1) {
- repeats = repeats - step + 1
- for(let k = 0; k < step; k++) {
- trigon += '#'
- }
- } else {
- trigon += '.'
- }
- }
- trigon += '\n'
- step += 2
- }
- console.log(trigon)
- //black belt
- let history = [1, 1, 1, 1]
- let predictArray = [
- [
- [[-1],[-1]],[[-1],[-1]]
- ],
- [
- [[-1],[-1]],[[-1],[-1]]
- ]
- ]
- while(true) {
- console.log('my prediction is:',
- predictArray[history[0]][history[1]][history[2]][history[3]] >= 0? predictArray[history[0]][history[1]][history[2]][history[3]]:
- Math.random() > 0.5? 1 : 0
- )
- let variant = prompt('input 1 or 0')
- if(variant === null) {
- break;
- } else if(+variant === 1 || +variant === 0) {
- predictArray[history[0]][history[1]][history[2]][history[3]] = +variant
- history.shift()
- history.push(+variant)
- }
- }
|