Skip to content

Instantly share code, notes, and snippets.

@NateWr
Forked from rarmatei/js2_test.md
Last active April 27, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NateWr/7c94bc5a3fab14c3f3778cef438c27b9 to your computer and use it in GitHub Desktop.
Save NateWr/7c94bc5a3fab14c3f3778cef438c27b9 to your computer and use it in GitHub Desktop.

What is the value result in these code snippets

// 1
var writerAge = 10
var result = writerAge + 5
20
// 2
var writer1 = 10
var writer2 = 20
var result = writer1 + writer2
// 3
var writer1 = "10"
var writer2 = "20"
var result = writer1 + writer2
// 4
var writerAge = 10
var result = writerAge > 20
// 5
var writerAge = 10
var result = writerAge === 10
// 6
var writer = "smith"
var result = typeof writer
// 7
var isWriter = true
var result = typeof isWriter
// 8
var writerAge = 10
var result = typeof writerAge
// 9
var writerName = undefined
var result = writerName || "Salih"
// 10
var writerName = "Marquez"
var result = writerName || "Salih"

lists and strings

// 11
var writer = "smith"
var result = writer.length
// 12
var writer = "smith"
var result = writer.split()
// 13
var writer = "achebe"
var result = writer.substring(0, 2)
// 14
var writer = "achebe"
var result = writer.toUpperCase()
// 15
var writer = "achebe"
var result = writer[1].toUpperCase()
// 16
var writer = "achebe"
var result = writer.split('')
// 17
var writer = ["a", "c", "h", "e", "b", "e"] 
var result = writer.join('')
// 18
var writer = ["a", "c", "h", "e", "b", "e"] 
var result = writer.reverse()
// 19
var writer = "mahfouz"
var result = writer.split('').reverse().join('')

functions

// 20
function doSomething(num) {
  return num * -1;
}

var result = doSomething(10)
// 21
function doSomething(num, num2) {
  return num - num2;
}
var num = 5
var result = doSomething(10, num)
// 22
function doSomething(num1, num2, num3) {
  if(num3 === true) {
  	return num1 - num2
  } else {
    return num1 + num2;
  }
}

// what are the values for result1, result2 an result3
var result1 = doSomething(10, 5, true)
var result2 = doSomething(10, 5, false)
var result2 = doSomething(10, 5)

Given this array of writers, what will the value of result be

var writers = [“marquez”, “morrisson”, “mahfouz”, “achebe”]
// 23
var result =  writers.length
// 24
var result = writers[1]
// 25
var result = writers.filter(function(writer){
	return writer.length > 5;
});
// 26 - values for result1 and result2
var result1 = writers.slice(0, 1)
var result2 = writers.slice()
// 27
writers[1] = "qabbani"
var result = writers;
// 28
var result = writers.map(function(writer) {
	if (writer.startsWith('m')) {
		return writer.toUpperCase();
	} else {
		return writer;
	}
})
// 29
var result = writers.includes(“orwell”)

Given this object, what is the value of result

var writer = {
	firstName: “Tayib”,
	lastName: “Salih”,
	alive: false
}
// 30
var result = Object.keys(writer)
// 31
var result = writer[0]
// 32 - values of result1, result2, result3
var result1 = writer.fullName
var result2 = writer['fullName']
var property = 'fullName'
var result3 = writer[property]

Given this array of objects

var writers = [{
	firstName: “Tayib”,
	lastName: “Salih”,
	alive: false
},  {
	firstName: “Gabriel”,
	lastName: “Marquez”,
	alive: false	
}, {
	firstName: “Zadie”
	lastName: “Smith”,
	alive: true
}]
// 33
var result = writers[0].name
// 34
var result = writers[1].firstName
// 35
var result = writers[1].firstName.length
// 36
var result = writers.map(function(writer) {
	return writer.lastName + ' ' + writer.firstName;
})
// 37
var result = writers.map(function(writer) {
	if (writer.alive) {
		return writer.lastName;
	} else {
		return "x"
	}
})
// 38
var result = writers.find(function(writer) {
	return writer.alive === false;
})
// 39
var result = writers.filter(function(writer) {
	return writer.alive === false;
})
// 40
var result = writers.find(function(writer) {
	return !writer.alive
})

DOM

Explain in detail using plain English what's happening in the code below:

var myButton = document.querySelector('#myButton');

  function alertSomething() {
      alert("Something");
  }

  myButton.addEventListener("click", alertSomething);

I'll start you off: "We are declaring a variable called myButton. We are then calling the querySelector method on the document object..."


Given the below HTML:

<div id="pageTitle"> 
	Welcome to my blog! 
</div>
<p class="post"> Ok, I'll write one more article </p>
<p id="firstPost" class="post"> This is the beginning of my blog... </p>

Which expressions below return you the paragraph with the text "This is the beginning of my blog..."

  • document.querySelector('.post')
  • document.querySelectorAll('.post')[0]
  • document.querySelector('#firstPost')
  • document.querySelectorAll('.post')[1]
  • document.getElementById('.post')
  • document.querySelector('.post#firstPost')

Now imagine you wanted to change the background colour of each post to blue, and you had to first explain your approach in English to another developer. Fill in the blanks in the following sentence:

"I will use the method ...................... on "document" and I will pass it the value .......... Because that method returns me an array, I will call the array method ......... on it and pass it the below function:

function setBgBlue(paragraph) {
    paragraph.style.backgroundColor = "blue";
}

Async

You are making a chat application. Describe at least 3 features that would be on the client and 3 features that would be on the server:

Client:

  • Show an input box and when someone types in it and presses "Submit" it should make a POST request to the server with the new message

  • .............

  • ..............

Server:

  • Listen for new message requests and add them to the existing lists of messages

  • ..............................

  • ...............................


We've written some code below that wishes Irina 'Happy birthday!' when it's her birthday. We first initialise her birthday variable as today but then we make a server request to get her REAL birthday. Do you see any problems with the code below or will it always work?

var today = '27/04/2019';
var irinasBirthday = '27/04/2019';

fetch('http://irina-api.com/my-birthday')
	.then(function (response) {
		return response.text();
	})
	.then(function (irinasActualBirthday) {
		irinasBirthday = irinasActualBirthday;
	});

if(today === irinasBirthday) {
	alert('Happy birthday!');
}

How would you fix it?


For each of the situations below, say whether you would use GET or POST for the request:

  • retrieve a list of blog posts
  • login to a site
  • send an email
  • perform a search on Google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment