Browse Source

js exercises: declaration merge

pull/560/head
lee 5 years ago committed by Clément
parent
commit
f2e91d469a
  1. 9
      js/tests/arr_test.js
  2. 75
      js/tests/declarations.js
  3. 22
      js/tests/escape-str_test.js
  4. 39
      js/tests/nested_test.js
  5. 22
      js/tests/obj_test.js
  6. 11
      subjects/arr.en.md
  7. 25
      subjects/declarations.en.md
  8. 17
      subjects/escape-str.en.md
  9. 15
      subjects/nested.en.md
  10. 15
      subjects/obj.en.md

9
js/tests/arr_test.js

@ -1,9 +0,0 @@
export const tests = []
const t = (f) => tests.push(f)
t(() => Array.isArray(arr)) // arr is declared and is an array
t(({ eq }) => eq(arr[0], 4)) // arr first element is 4
t(({ eq }) => eq(arr[1], '2')) // arr second element is "2"
t(({ eq }) => eq(arr.length, 2)) // arr length is 2
Object.freeze(tests)

75
js/tests/declarations.js

@ -0,0 +1,75 @@
export const tests = []
const t = (f) => tests.push(f)
// escapeStr
// is declared and of type string
t(() => typeof escapeStr === 'string')
// should include the character '
t(() => escapeStr.includes("'"))
// should include the character "
t(() => escapeStr.includes('"'))
// should include the character `
t(() => escapeStr.includes('`'))
// should include the character /
t(() => escapeStr.includes('/'))
// should include the character \
t(() => escapeStr.includes('\\'))
// arr
t(() => Array.isArray(arr)) // arr is declared and is an array
t(({ eq }) => eq(arr[0], 4)) // arr first element is 4
t(({ eq }) => eq(arr[1], '2')) // arr second element is "2"
t(({ eq }) => eq(arr.length, 2)) // arr length is 2
// obj
// obj is declared and of type object
t(() => obj.constructor === Object)
// obj is constant and can not be re-assigned
t(({ fail }) => obj && fail(() => (obj = 10)))
// obj.str is of type string
t(() => typeof obj.str === 'string')
// obj.num is of type number
t(() => typeof obj.num === 'number')
// obj.bool is of type boolean
t(() => typeof obj.bool === 'boolean')
// obj.undef is of type undefined
t(() => 'undef' in obj && typeof obj.undef === 'undefined')
// nested
const cantEdit = (fn) => {
try {
fn()
} catch (err) {
return true
}
}
t(() => nested.constructor === Object)
// nested is constant and can not be re-assigned
t(({ fail }) => nested && fail(() => (nested = 10)))
t(() => nested.obj.constructor === Object)
t(() => typeof nested.obj.str === 'string')
t(() => typeof nested.obj.num === 'number')
t(() => typeof nested.obj.bool === 'boolean')
t(() => Array.isArray(nested.arr))
t(() => nested.arr[0] === 4)
t(() => nested.arr[1] === undefined)
t(() => nested.arr[2] === '2')
t(() => nested.arr.length === 3)
// nested is frozen and can not be changed
t(() => cantEdit(() => (nested.obj = 5)))
t(() => nested.obj !== 5)
// nested.obj is also frozen and can not be changed
t(() => cantEdit(() => (nested.obj.update = 5)))
t(() => nested.obj.update === undefined)
// nested.arr is not frozen and can be changed
t(() => cantEdit(() => nested.arr.push('hot stuff')))
t(() => nested.arr.length === 3)
Object.freeze(tests)

22
js/tests/escape-str_test.js

@ -1,22 +0,0 @@
export const tests = []
const t = (f) => tests.push(f)
// escapeStr is declared and of type string
t(() => typeof escapeStr === 'string')
// escapeStr should include the character '
t(() => escapeStr.includes("'"))
// escapeStr should include the character "
t(() => escapeStr.includes('"'))
// escapeStr should include the character `
t(() => escapeStr.includes('`'))
// escapeStr should include the character /
t(() => escapeStr.includes('/'))
// escapeStr should include the character \
t(() => escapeStr.includes('\\'))
Object.freeze(tests)

39
js/tests/nested_test.js

@ -1,39 +0,0 @@
export const tests = []
const t = (f) => tests.push(f)
const cantEdit = (fn) => {
try {
fn()
} catch (err) {
return true
}
}
t(() => nested.constructor === Object)
// nested is constant and can not be re-assigned
t(({ fail }) => nested && fail(() => (nested = 10)))
t(() => nested.obj.constructor === Object)
t(() => typeof nested.obj.str === 'string')
t(() => typeof nested.obj.num === 'number')
t(() => typeof nested.obj.bool === 'boolean')
t(() => Array.isArray(nested.arr))
t(() => nested.arr[0] === 4)
t(() => nested.arr[1] === undefined)
t(() => nested.arr[2] === '2')
t(() => nested.arr.length === 3)
// nested is frozen and can not be changed
t(() => cantEdit(() => (nested.obj = 5)))
t(() => nested.obj !== 5)
// nested.obj is also frozen and can not be changed
t(() => cantEdit(() => (nested.obj.update = 5)))
t(() => nested.obj.update === undefined)
// nested.arr is not frozen and can be changed
t(() => cantEdit(() => nested.arr.push('hot stuff')))
t(() => nested.arr.length === 3)
Object.freeze(tests)

22
js/tests/obj_test.js

@ -1,22 +0,0 @@
export const tests = []
const t = (f) => tests.push(f)
// obj is declared and of type object
t(() => obj.constructor === Object)
// obj is constant and can not be re-assigned
t(({ fail }) => obj && fail(() => (obj = 10)))
// obj.str is of type string
t(() => typeof obj.str === 'string')
// obj.num is of type number
t(() => typeof obj.num === 'number')
// obj.bool is of type boolean
t(() => typeof obj.bool === 'boolean')
// obj.undef is of type undefined
t(() => 'undef' in obj && typeof obj.undef === 'undefined')
Object.freeze(tests)

11
subjects/arr.en.md

@ -1,11 +0,0 @@
## Arr
### Instructions
Create a constant variable named `arr` that is an array
containing the values 4 and '2'
### Notions
- [nan-academy.github.io/js-training/examples/data-structures.js](https://nan-academy.github.io/js-training/examples/data-structures.js)

25
subjects/declarations.en.md

@ -0,0 +1,25 @@
## Declarations
### Instructions
Create the following variables:
- `escapeStr`, that contains the following specials characters: \`, `\`, `/`, `"`, `'`
- `arr` that is an array containing the values 4 and '2'
- `obj` that is an object containing a property for each primitives:
- `str` for `String`
- `num` for `Number`
- `bool` for `Boolean`
- `undef` for `undefined`
- `nested` that contains
- arr: an array of the 3 values: 4, undefined, '2'
- obj: an object with 3 property (str, num & bool)
`nested` and `obj` must be frozen to prevent changes from them.
### Notions
- Primitive and Operators
- Variables
- [nan-academy.github.io/js-training/examples/data-structures.js](https://nan-academy.github.io/js-training/examples/data-structures.js)
- [devdocs.io/javascript/global_objects/object/freeze](https://devdocs.io/javascript/global_objects/object/freeze)

17
subjects/escape-str.en.md

@ -1,17 +0,0 @@
## Escape Str
### Instructions
Create a constant variable named `escapeStr` that contains
the following specials characters:
- `
- \
- /
- "
- '
### Notions
- Primitive and Operators
- Variables

15
subjects/nested.en.md

@ -1,15 +0,0 @@
## Nested
### Instructions
Create a `nested` constant variable that contains
- arr: an array of the 3 values: 4, undefined, '2'
- obj: an object with 3 property (str, num & bool)
`nested` and `obj` must be frozen to prevent changes from them.
### Notions
- [nan-academy.github.io/js-training/examples/data-structures.js](https://nan-academy.github.io/js-training/examples/data-structures.js)
- [devdocs.io/javascript/global_objects/object/freeze](https://devdocs.io/javascript/global_objects/object/freeze)

15
subjects/obj.en.md

@ -1,15 +0,0 @@
## Obj
### Instructions
Create a constant variable named `obj` that is an object
containing a property for each primitives:
- `str` for `String`
- `num` for `Number`
- `bool` for `Boolean`
- `undef` for `undefined`
### Notions
- [nan-academy.github.io/js-training/examples/data-structures.js](https://nan-academy.github.io/js-training/examples/data-structures.js)
Loading…
Cancel
Save