javascript Javascript基础知识

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Javascript基础知识相关的知识,希望对你有一定的参考价值。

// concise body syntax, implied "return"
var func = x => x * x;                

// example function of consice body syntax
const areaOfCircle = radius => Math.PI * radius * radius;
areaOfCircle(4);

//Block body, explicit "return" needed
var func = (x, y) => { return x + y; }; 


const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
    return userInput;
  } else {
    console.log('invalid input');
  }
  
}

const getComputerChoice = () => {
  let ranval = Math.floor(Math.random() * 4);
  switch(ranval){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
    case 3:
      return 'bomb';
               }
}

const determineWinner = (userChoice, computerChoice) =>{
  if(userChoice === 'bomb'){
    return 'user won';
  } else if (computerChoice === 'bomb'){
    return 'computer won';
  }
  
  if(userChoice === computerChoice){
    return "Game was a tie."
  }
  
  if(userChoice === 'rock'){
    if(computerChoice === 'scissors'){
      return `user won with ${userChoice} over ${computerChoice}`;
    } else if (computerChoice === 'paper'){
      return `computer won with ${computerChoice} over ${userChoice}`;
    }
  }
  
  if(userChoice === 'paper'){
    if(computerChoice === 'scissors'){
      return 'computer won';
    } else if (computerChioce === 'rock'){
      return 'user won';
    }
  }
  
  if(userChoice === 'scissors'){
    if(computerChoice === 'rock'){
      return 'computer won';
    } else if (computerChoice === 'paper'){
      return 'user won';
    }
  }
}

const playGame = () =>{
	let userChoice = getUserChoice('rock');
  console.log(userChoice);
  let computerChoice = getComputerChoice();
  console.log(computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
// function decloration
function isGreaterThan(numberOne, numberTwo){
  if(numberOne > numberTwo){
     return true;
     } else {
       return false;
     }
};

// this is an arrow expression
const isGreaterThan = (numberOne, numberTwo) => {
  if(numberOne > numberTwo){
    return true;
  } else {
    return false;
  }
}

isGreaterThan(11, 12);
let cards = ['Diamond', 'Spade', 'Heart', 'Club'];
let currentCard = 'Heart';

while(currentCard !== 'Spade'){
  console.log(currentCard);
  currentCard = cards[Math.floor(Math.random() * 4)];
}

console.log('Program found a spade');
// this function increments a variable every time it is used
// functions should be returning values
// start at zero
let orderCount = 0;

const takeOrder = (topping, crustType) => {
  // every time this function runs its getting bigger
  orderCount = orderCount + 1;
  console.log(`Order: ${crustType} crusty pizza with ${topping}`);
};

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};

takeOrder("mushrooms", "Gluten Free");
takeOrder("snosages", "Chinese");
takeOrder("Noodle", "Garlic");
console.log(getSubTotal(orderCount));
let calculatorIsOn = false;

const pressPowerButton = () => {
  if (calculatorIsOn) {
    console.log('Calculator turning off.');
    calculatorIsOn = false;
  } else {
    console.log('Calculator turning on.');
    calculatorIsOn = true;
  }
};

pressPowerButton();
// Output: Calculator turning on.

pressPowerButton();
// Output: Calculator turning off.
let secretMessage = ["Learning", "isn't", "about", "what", "you", "get", "easily", "the", "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.", "-2015,", "Chris", "Pine,", "Learn", "JavaScript"];

secretMessage.pop();
secretMessage.push('to', 'program');
secretMessage[6] = 'right';
secretMessage.shift();
secretMessage.unshift('Programming');
secretMessage.splice(6, 4, 'know');
console.log(secretMessage.join(" "));
let groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];

groceryList.shift();
groceryList.unshift('popcorn');

console.log(groceryList.slice(1, 4));
console.log(groceryList);
// Back tick
var kelvin = 294;

var celsius = kelvin - 273;

fahrenheit = celsius * (9/5) + 32;

fahrenheit = Math.floor(fahrenheit);

console.log(`the temperature is ${fahrenheit} degrees fahrenheit.`);
// the ` backtick will allow you to do string interpolation



// simple javascript array
let newYearsResolutions = [ 'code like a master', 
                            'be an amazing influence and mentor', 
                            'Work hard everyday'];

newYearsResolutions.push('Keep practicing');
newYearsResolutions.push('peanut butter');
newYearsResolutions.pop();

// other array methods
// .map() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
// .join() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
// .slice() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
// arr.slice([begin[, end]])
// .shift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
// removes the first item in the list

// .unshift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift



// add items to the front of the list

// .concat() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
let isLocked = false;

isLocked ? console.log('You will need a key to open the door.') : console.log('You will not need a key to open the door.');

let isCorrect = true;

isCorrect ? console.log('Correct!') : console.log('Incorrect!');


let favoritePhrase = 'Love That!';

favoritePhrase === 'Love That!' ? console.log('I love that!') : console.log("I don't love that!");

let moonPhase = 'full';

switch(moonPhase) {
	case 'full':
    console.log('Howl!');
    break;
  case 'mostly full':
  	console.log('Arms and legs are getting hairier');
  	break;
  case 'mostly new':
  	console.log('Back on two feet');
  	break;
  default:
  	console.log('Invalid moon phase');
    break;
}
if (!favoritePhrase) {
  console.log("This string doesn't seem to be empty.");
} else {
  console.log('This string is definitely empty.');
}

以上是关于javascript Javascript基础知识的主要内容,如果未能解决你的问题,请参考以下文章

javascript Javascript的基础知识

javascript javascript基础知识

javascript JavaScript基础知识

javascript JavaScript语言基础知识

javascript Javascript基础知识

JavaScript学习---JavaScript基础知识