freecode Basic JavaScript(上)

1.Comment Your JavaScript Code

//this is a comment
/*this also a comment */

2.Declare JavaScript Variables

var myName;

3.Storing Values with the Assignment Operator

// Setup
var a;
a=7;

// Only change code below this line

4.Assigning the Value of One Variable to Another

// Setup
var a;
a = 7;
var b;
b=a;
// Only change code below this line

5.Initializing Variables with the Assignment Operator

var a=9;

6.Understanding Uninitialized Variables

// Only change code below this line
var a=5;
var b=10;
var c='I am a';

// Only change code above this line

a = a + 1;
b = b + 5;
c = c + " String!";

7.Understanding Case Sensitivity in Variables

//Variable declarations
var studlyCapVar;
var properCamelCase;
var titleCaseOver;

// Variable assignments
studlyCapVar = 10;
properCamelCase = "A String";
titleCaseOver = 9000;

8.Add Two Numbers with JavaScript

var sum = 10 + 10;

9.Subtract One Number from Another with JavaScript

var difference = 45 - 33;

10.Multiply Two Numbers with JavaScript

var product = 8 * 10;

11.Divide One Number by Another with JavaScript

var quotient = 66 / 33;

12.Increment a Number with JavaScript

var myVar = 87;

// Only change code below this line
myVar++;
myVar;

13.Decrement a Number with JavaScript

var myVar = 11;

// Only change code below this line
myVar --;
myVar;

14.Create Decimal Numbers with JavaScript

var ourDecimal = 5.7;

// Only change code below this line
var myDecimal = 5.7;

15.Multiply Two Decimals with JavaScript

var product = 2.0 * 2.5;

16.Divide One Decimal by Another with JavaScript

var quotient = 4.4 / 2.0; // Change this line

17.Finding a Remainder in JavaScript

// Only change code below this line

var remainder=11%3;

18.Compound Assignment With Augmented Addition

var a = 3;
var b = 17;
var c = 12;

// Only change code below this line
a += 12;
b += 9;
c += 7;

19.Compound Assignment With Augmented Subtraction

var a = 11;
var b = 9;
var c = 3;

// Only change code below this line
a -= 6;
b -= 15;
c -= 1;

20.Compound Assignment With Augmented Multiplication

var a = 5;
var b = 12;
var c = 4.6;

// Only change code below this line
a *=  5;
b *= 3 ;
c *= 10;

21.Compound Assignment With Augmented Division

var a = 48;
var b = 108;
var c = 33;

// Only change code below this line
a /= 12;
b /= 4;
c /=  11;

22.Declare String Variables

var myFirstName="aaa";
var myLastName="bbb";

23.Escaping Literal Quotes in Strings

var myStr = "I am a \"double quoted\" string inside \"double quotes\"."; // Change this line

24.Quoting Strings with Single Quotes

var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';

25.Escape Sequences in Strings

var myStr="FirstLine\n\t\\SecondLine\nThirdLine"; // Change this line

26.Concatenating Strings with Plus Operator

var myStr="This is the start. " + "This is the end.";; // Change this line

27.Concatenating Strings with the Plus Equals Operator

// Only change code below this line

var myStr='This is the first sentence. ';
myStr += 'This is the second sentence.';

28.Constructing Strings with Variables

// Only change code below this line
var myName="aaabbb";
var myStr='My name is '+myName+'and I am well!';

29.Appending Variables to Strings

// Change code below this line

var someAdjective="bbb";
var myStr = "Learning to code is ";
myStr += someAdjective;

30.Find the Length of a String

// Setup
var lastNameLength = 0;
var lastName = "Lovelace";

// Only change code below this line

lastNameLength = lastName.length;

31.Use Bracket Notation to Find the First Character in a String

// Setup
var firstLetterOfLastName = "";
var lastName = "Lovelace";

// Only change code below this line
firstLetterOfLastName = lastName[0]; // Change this line

32.Understand String Immutability

// Setup
var myStr = "Jello World";

// Only change code below this line
myStr = "Hello World"; // Change this line
// Only change code above this line

33.Use Bracket Notation to Find the Nth Character in a String

// Setup
var lastName = "Lovelace";

// Only change code below this line
var thirdLetterOfLastName = lastName[2]; // Change this line

34.Use Bracket Notation to Find the Last Character in a String

// Setup
var lastName = "Lovelace";

// Only change code below this line
var lastLetterOfLastName = lastName[lastName.length-1]; // Change this line

35.Use Bracket Notation to Find the Nth-to-Last Character in a String

// Setup
var lastName = "Lovelace";

// Only change code below this line
var secondToLastLetterOfLastName = lastName[lastName.length-2]; // Change this line

36.Word Blanks

var myNoun = "dog";
var myAdjective = "big";
var myVerb = "ran";
var myAdverb = "quickly";

// Only change code below this line
var wordBlanks = myNoun+","+myVerb+","+myAdjective+","+myAdverb; // Change this line
// Only change code above this line

37.Store Multiple Values in one Variable using JavaScript Arrays

// Only change code below this line
var myArray = ['aaa',4];

38.Nest one Array within Another Array

// Only change code below this line
var myArray = [['aaa',1],['bbb',2]];

39.Access Array Data with Indexes

var myArray = [50,60,70];
var myData =myArray[0];

40.Modify Array Data With Indexes

// Setup
var myArray = [18,64,99];

// Only change code below this line
myArray[0]=45;

41.Access Multi-Dimensional Arrays With Indexes

var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

var myData = myArray[2][1];

42.Manipulate Arrays With push()

// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line
myArray.push(["dog", 3])

43.Manipulate Arrays With pop() //删除最后一个元素

// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line
var removedFromMyArray=myArray.pop();

44.Manipulate Arrays With shift() //删除第一个元素

// Setup
var myArray = [["John", 23], ["dog", 3]];

// Only change code below this line
var removedFromMyArray=myArray.shift();

45.Manipulate Arrays With unshift() //添加第一个元素

// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();

// Only change code below this line
myArray.unshift(["Paul",35])

46.Shopping List

var myList = [["aaa",34],["bbb",45],['ddd',78],["ccc",12,"jsj"],["js",67]];

47.Write Reusable JavaScript with Functions

function reusableFunction(){
  console.log("Hi World");
}
reusableFunction()

48.Passing Values to Functions with Arguments

function functionWithArgs(a,b){
  console.log(a+b)
}
functionWithArgs(12,4) 

49.Global Scope and Functions

// Declare the myGlobal variable below this line
var myGlobal =10;
let oopsGlobal;

function fun1() {
  // Assign 5 to oopsGlobal Here
  oopsGlobal=5;
}
// Only change code above this line

function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

50.Local Scope and Functions

function myLocalScope() {

  // Only change code below this line
  var myVar=8;
  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

51.Global vs. Local Scope in Functions

// Setup
var outerWear = "T-Shirt";

function myOutfit() {
  // Only change code below this line
  var outerWear="sweater";
  // Only change code above this line
  return outerWear;
}

myOutfit();

52.Return a Value from a Function with Return

function timesFive(num){
  return num*5;
}  

53.Understanding Undefined Value returned from a Function

// Setup
var sum = 0;

function addThree() {
  sum = sum + 3;
}

// Only change code below this line
function addFive(){
  sum = sum+5
} 
// Only change code above this line

addThree();
addFive();

  • 28
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值