Skip to content

Instantly share code, notes, and snippets.

@Sarah-JJ
Last active February 5, 2018 19:09
Show Gist options
  • Save Sarah-JJ/48a287c1ad47797d08f3848574669f2c to your computer and use it in GitHub Desktop.
Save Sarah-JJ/48a287c1ad47797d08f3848574669f2c to your computer and use it in GitHub Desktop.
//1-objects
var car = {model: "Honda", number: 35324};
car.number = 754242;
function printProperty(obj){
console.log(obj.model +"'s plate number is "+ obj.number);
}
printProperty(car);
//2-if and switch
var x=3, y="3";
if(x == y)
console.log("x and y are equal, but might not have similar types.");
if(x===y)
console.log("x and y are equal, and have similar types");
else
console.log("the condition was false");
//getting user input for the variable day
var day = parseInt(prompt("Enter a day number between 1 and 7:"));
//in case a user enters a number other than 1-7
if (day<0 || day>7)
day = parseInt(prompt("The number must be between 1 and 7:"));
switch(day){
case 1: console.log("Sunday"); break;
case 2: console.log("Monday"); break;
case 3: console.log("Tuesday"); break;
case 4: console.log("Wednesday"); break;
case 5: console.log("Thursday"); break;
case 6: console.log("Friday"); break;
case 7: console.log("Saturday"); break;
}
//3-for loop to print object properties through an array
var obj1 = {name: "Objy1", number: 1};
var obj2 = {name: "Objy2", number: 2};
var obj3 = {name: "Objy3", number: 3};
var arr = [obj1, obj2, obj3];
for(var i=0; i<arr.length; i++)
console.log(arr[i].name +"'s number is " + arr[i].number );
//4-functions
//sum function
function sum(a, b){
return (a+b);
}
console.log("Sum = " + sum(3, 4));
//adding an element to an array and returning the new array
var myArr = ["a", "b", "c"];
function addingElement(arr){
var newValue = prompt("Enter the value you want to add to the array:");
arr.push(newValue);
return (arr);
}
//call the function which returns the array and console log it
console.log("The array returned from the function is " + addingElement(myArr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment