main.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Temperature
  2. /*
  3. {
  4. const far = (t) => ((t * 9/5) + 32)
  5. alert(far(0) + " F")
  6. }
  7. //RGB
  8. {
  9. const red = +prompt ("Write your value for red:")
  10. const green = +prompt ("Write your value for green :")
  11. const blue = +prompt ("Write your value for blue :")
  12. const f = (r, g, b) => {
  13. r = r.toString(16)
  14. if(r.length < 2) {
  15. r = r+r
  16. }
  17. g = g.toString(16)
  18. if(g.length < 2) {
  19. g = g + g
  20. }
  21. b = b.toString(16)
  22. if(b.length < 2) {
  23. b = b + b
  24. }
  25. let result = r+g+b
  26. alert (`Your color: #${result}`)
  27. }
  28. f(red, green, blue);
  29. }
  30. //FLATS
  31. {
  32. let floors = +prompt('Write amount of floors')
  33. let flatsInFloor = +prompt('Write amount of flats in floor')
  34. const answerForFlats = (floor,flats) => {
  35. let numberOfFlat = +prompt('Write number of flat')
  36. let remainderFlat = (numberOfFlat) % (floor * flats)
  37. let entrance = Math.ceil((numberOfFlat)/(floor * flats))
  38. let numberOfFloor = Math.ceil(remainderFlat / flats);
  39. let result = {entrance, numberOfFloor}
  40. return(result)
  41. }
  42. console.log(answerForFlats(floors, flatsInFloor))
  43. }
  44. //Credentials
  45. let functionForName = () => {
  46. const capitalize = str => {
  47. let result = str.split('')[0].toUpperCase() + str.slice(1,+Infinity).toLowerCase()
  48. return result //именно этот код обеспечит возврат результата функции
  49. }
  50. let firstname = capitalize(prompt("Write your name"))
  51. let surname = capitalize(prompt("Write your surname"))
  52. let fathername = capitalize(prompt("Write your thirdname"))
  53. let fullname = (firstname + " " + surname + " " + fathername)
  54. let result = {firstname, surname, fathername, fullname}
  55. return result
  56. }
  57. console.log(functionForName())
  58. //New line
  59. {
  60. const functionForLine = l => l.split('\\n').join('\n')
  61. console.log(functionForLine( prompt("Write some text,you can use (\\n) for add new line")))
  62. }
  63. //Prompt OR
  64. {
  65. const functionForAge = (age=0) => +prompt("How old are you?")
  66. console.log(2022 - functionForAge())
  67. }
  68. //Login and password
  69. {
  70. const functionForLoginAndPassword = (login,password) => {
  71. let result = (login === "admin") && (password==="qwerty") ? true : false
  72. return result
  73. }
  74. console.log(functionForLoginAndPassword(prompt("login"), prompt("password")))
  75. }
  76. //For Table
  77. {
  78. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  79. const functionForArrInArr = arr => {
  80. let str = "<table>"
  81. let i = 0
  82. for (let number of arr){
  83. str += "<tr>" + "<tr/>"
  84. for (let letter of number){
  85. str += (i % 2) ? "<td style=background-color:#FF0000>" + letter + "<td>" : "<td style=background-color:#000FFF>" + letter + "<td>"
  86. }
  87. i++
  88. }
  89. return document.write(str)
  90. }
  91. console.log(functionForArrInArr(arr))
  92. }
  93. //Filter Lexics
  94. {
  95. const answer = prompt("Напиши шось").split(" ")
  96. const badWords = ["бляха", "пиздос", "лох", "шабля"]
  97. const funForFilter = (str, badStr) => {
  98. let strFilter = str.map(x=> badStr.includes(x) ? "" : x ).join(" ")
  99. return strFilter
  100. }
  101. console.log(funForFilter(answer, badWords))
  102. }
  103. */
  104. //Currency table
  105. {
  106. const functionForArrInArr = arr => {
  107. let str = "<table>"
  108. let i = 0
  109. for (let number of arr){
  110. str += "<tr>" + "<tr/>"
  111. for (let letter of number){
  112. str += (i % 2) ? "<td style=background-color:#FF0000>" + letter + "<td>" : "<td style=background-color:#000FFF>" + letter + "<td>"
  113. }
  114. i++
  115. }
  116. return document.write(str)
  117. }
  118. const functionForJson = () => { fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  119. .then(data => {
  120. const key = 0
  121. const value = 1
  122. const arr = Object.entries( data.rates )
  123. let str = "<table border=1px>"
  124. str += "<tr>" + "<td>" + "</td>"
  125. for (let i = 0; i<arr.length; i++){
  126. str += `<td> ${arr[i][key]} </td>`
  127. }
  128. str += "</tr>"
  129. for (let i = 0; i<arr.length; i++){
  130. str += `<tr><td> ${arr[i][key]} </td>`
  131. rate1 = arr[i][value]
  132. for(let j=0; j<arr.length; j++){
  133. rate2 = arr[j][value]
  134. crossRate = rate2 / rate1
  135. str += `<td> ${crossRate.toFixed(2)} </td>`
  136. }
  137. }
  138. str += "</tr>"
  139. str += "</table>"
  140. console.log(data)
  141. //document.write(str)
  142. })
  143. document.write(functionForArrInArr(functionForJson(arr)))
  144. }
  145. console.log(functionForArrInArr(functionForJson()))
  146. }
  147. //??????????????????????????????????????????????????????????????????????
  148. /*
  149. // Form
  150. {
  151. const car = {
  152. "Name":"chevrolet chevelle malibu",
  153. "Cylinders":8,
  154. "Displacement":307,
  155. "Horsepower":130,
  156. "Weight_in_lbs":3504,
  157. "Origin":"USA",
  158. "in_production": false
  159. }
  160. const createObject = obj => {
  161. let str = "<form>"
  162. for ( const [key,value] of Object.entries(obj)){
  163. if (typeof(value) === "string"){
  164. str += `<label>${key}: <input type="text" value="${value}" /> </label> `
  165. }
  166. if(typeof(value) === "number"){
  167. str += `<label>${key}: <input type="number" value="${value}" /> </label> `
  168. }
  169. if(typeof(value) === "boolean"){
  170. str += `<label>${key}: <input type="checkbox" value="${value}" /> </label> `
  171. }
  172. }
  173. str += "</form>"
  174. return document.write(str)
  175. }
  176. console.log(createObject(car))
  177. }
  178. //Array of objects sort
  179. {
  180. var persons = [
  181. {name: "Иван", age: 17},
  182. {name: "Мария", age: 35},
  183. {name: "Алексей", age: 73},
  184. {name: "Яков", age: 12},
  185. ]
  186. const funSort = (arr, name, booln=true) => {
  187. let result = arr.sort((first,second) => (first[name] < second[name]) == booln ? -1 : 1)
  188. return result
  189. }
  190. console.log(funSort(persons, "age"))
  191. console.log(funSort(persons, "age", false))
  192. }
  193. //Table
  194. {
  195. const persons =
  196. [
  197. {
  198. "Name":"chevrolet chevelle malibu",
  199. "Cylinders":8,
  200. "Displacement":307,
  201. "Horsepower":130,
  202. "Weight_in_lbs":3504,
  203. "Origin":"USA"
  204. },
  205. {
  206. "Name":"buick skylark 320",
  207. "Miles_per_Gallon":15,
  208. "Cylinders":8,
  209. "Displacement":350,
  210. "Horsepower":165,
  211. "Weight_in_lbs":3693,
  212. "Acceleration":11.5,
  213. "Year":"1970-01-01",
  214. },
  215. {
  216. "Miles_per_Gallon":18,
  217. "Cylinders":8,
  218. "Displacement":318,
  219. "Horsepower":150,
  220. "Weight_in_lbs":3436,
  221. "Year":"1970-01-01",
  222. "Origin":"USA"
  223. },
  224. {
  225. "Name":"amc rebel sst",
  226. "Miles_per_Gallon":16,
  227. "Cylinders":8,
  228. "Displacement":304,
  229. "Horsepower":150,
  230. "Year":"1970-01-01",
  231. "Origin":"USA"
  232. },
  233. ]
  234. let personsCopy = []
  235. personsCopy = [...persons]
  236. const sortObj = (obj, line, booln = true) => {
  237. let result = obj.sort((a,b) => (a[line] < b[line]) == booln ? -1 : 1 )
  238. return result
  239. }
  240. console.log(sortObj(personsCopy, "Displacement"))
  241. {
  242. let i = 0
  243. let arrKeys = []
  244. for (let key of persons){
  245. arrKeys += Object.keys(persons[i]) + ","
  246. i++
  247. }
  248. arrKeys = arrKeys.split(",").slice(0,-1)
  249. const arrFilter = (x) => {
  250. return x.filter((el1,el2) => x.indexOf(el1) === el2 )
  251. }
  252. arrKeys = arrFilter(arrKeys)
  253. let arrValue =[]
  254. for (let arrVal of persons){
  255. arrValue.push(arrKeys.map(y=>arrVal[y] === undefined? arrVal[y]="-" : arrVal[y]))
  256. }
  257. arrValue.unshift(arrKeys)
  258. let str = `<table border="1">`
  259. for( let firstString of arrValue){
  260. str += `<tr>`
  261. for(let otherString of firstString){
  262. str += `<td>${otherString}</td>`
  263. }
  264. str += " </tr>"
  265. }
  266. str += "</table>"
  267. document.write(str)
  268. }
  269. let i = 0
  270. let arrKeys = []
  271. for (let key of personsCopy){
  272. arrKeys += Object.keys(persons[i]) + ","
  273. i++
  274. }
  275. arrKeys = arrKeys.split(",").slice(0,-1)
  276. const arrFilter = (x) => {
  277. return x.filter((el1,el2) => x.indexOf(el1) === el2 )
  278. }
  279. arrKeys = arrFilter(arrKeys)
  280. let arrValue =[]
  281. for (let arrVal of personsCopy){
  282. arrValue.push(arrKeys.map(y=>arrVal[y] === undefined? arrVal[y]="-" : arrVal[y]))
  283. }
  284. arrValue.unshift(arrKeys)
  285. let str = `<table border="1">`
  286. for( let firstString of arrValue){
  287. str += `<tr>`
  288. for(let otherString of firstString){
  289. str += `<td>${otherString}</td>`
  290. }
  291. str += " </tr>"
  292. }
  293. str += "</table>"
  294. document.write(str)
  295. }
  296. //Divide in "divide.html"
  297. //Calc Func
  298. {
  299. const firstTask = (cost,amount,smokeDay) => {
  300. const oneCigar = cost / amount
  301. const month = smokeDay * 30
  302. const moneyMonth = month * oneCigar
  303. let result = {
  304. first: oneCigar,
  305. second: month,
  306. third: moneyMonth
  307. }
  308. return result
  309. }
  310. firstTask(70, 20, +prompt("Write how cigarets you smoke evedy day:"))
  311. }
  312. */
  313. //Calc Live
  314. //Используя пример из задания Divide и функцию из Calc Func сделайте несколько полей ввода в HTML,
  315. //меняя которые вы будете получать результат калькуляции в каком-то div.