Get help on our basic JavaScript and Algorithms Challenges. If you are posting code that is large use Gist - https://gist.github.com/ paste the link here.
var myArray = [["Hello", 420], ["why", 69.1]];
myArray[0][0]
myArray[which element?][which item?0
select first element then second 0
pick "Hello" from that element
Array[which element do you want to select?][which value from this element?]
john-freecodecamp sends brownie points to @mctwodigitzero and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:warning: john-freecodecamp already gave mctwodigitzero points
:star2: 8010 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
sjames1958gm sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6210 | @manish-giri |http://www.freecodecamp.com/manish-giri
.add()
to get the skycon added. For example, add this after you .append()
in your $.each
loop:icons.add(document.getElementById(icon), icon)
.add(canvas-element, weather-condition-from-dark-sky)
Normally you want to avoid exposing API keys on CodePen
var count = 0;
function cc(card) {
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1;
break;
}
if(count>0){
card=" Bet";
}else if(count<=0){
card=" Hold";
} // If it possible use switch to achieve this like above????
return count + card;
}
cc(2); cc(3); cc(4); cc(5); cc(6);
this.setState()
active
property in every cell's state to off if I hit the clear button
function countTo(int) {
for (var i = 1; i < int + 1; i++) {
console.log(i);
}
}
Could someone tell me why this function doesn't work? I'm not sure what I'm missing, it's just supposed to log integers to the console, starting with 1 and going until int
erichasegawa sends brownie points to @njanne19 :sparkles: :thumbsup: :sparkles:
:cookie: 326 | @njanne19 |http://www.freecodecamp.com/njanne19
fdemaa sends brownie points to @njanne19 :sparkles: :thumbsup: :sparkles:
:cookie: 327 | @njanne19 |http://www.freecodecamp.com/njanne19
$(document).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
});
}
text
right beside the ? in the URL like so
https://twitter.com/intent/tweet?text=
yuserahn sends brownie points to @marcusparsons :sparkles: :thumbsup: :sparkles:
:cookie: 428 | @marcusparsons |http://www.freecodecamp.com/marcusparsons
encodeURIComponent()
with strings being sent in a GET request (like what you're doing with Twitter) and not relying upon the browser to encode them.
document.getElementById("tweetQuote").setAttribute("href", "https://twitter.com/intent/tweet?text=" + encodeURIComponent(randQuote));
njanne19 sends brownie points to @marcusparsons :sparkles: :thumbsup: :sparkles:
:cookie: 429 | @marcusparsons |http://www.freecodecamp.com/marcusparsons
function myReplace(str, before, after) {
var strArr=str.split(" ");
var afterArr=after.split("");
for(var i=0;i<strArr.length;i++){
if(strArr[i]==before){
if(strArr[i].toLowerCase()!=strArr[i]){
var sliced=afterArr.slice(0).join("");
var convertedAfter=afterArr[0].toUpperCase+spliced;
strArr[i]=convertedAfter;
return strArr.join("");
}
else{
strArr[i]=after;
return strArr.join;
}
}
}
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
This is Search and Replace in Intermediate Algorithm Scripting, my code can't pass. In fact, it keeps saying "testing challenge..." and there's no response to my code.
dmsdesignz sends brownie points to @marcusparsons :sparkles: :thumbsup: :sparkles:
:cookie: 431 | @marcusparsons |http://www.freecodecamp.com/marcusparsons
join(" ")
var parentt = document.getElementsByTagName("div")[0];
var referenceChild = document.getElementsByTagName("p")[0];
var newElement = document.createElement("h1");
var creationOfText = document.createTextNode("Hello World");
var result = parentt.insertBefore(newElement, referenceChild);
this.props.cells[this.props.id] = this;
$(events).on("calcNextMove", this.calcNextMove());
$(events).on("makeNextMove", this.makeNextMove());
I'm trying to get this.props.cells[this.props.id]
to equal the current Component that I am in. Am I doing it correctly or no?
function guessNumber(number) {
// Prompt the user for a number
var guess = prompt("Guess a number between 1 and 100");
// Convert their guess to a number using +
guess = +guess;
// Define base case
if (isNaN(guess) === true) {
return console.log("You got it! The number was " + number);
}
// Define recursive case with a function call
guessNumber(number);
}
// Call the function guessNumber() with an integer for an argument
guessNumber(7);
Can anyone tell me why the prompt just keeps popping up? It doesn't go away
erichasegawa sends brownie points to @njanne19 :sparkles: :thumbsup: :sparkles:
:cookie: 328 | @njanne19 |http://www.freecodecamp.com/njanne19
b
is undefined:function a() {
return console.log('hi');
}
var b = a();
console.log(b);
$.each($('canvas'), function(i, canvas) {
icons.add(canvas, $(canvas).attr('class'));
});
$("body").css("background-image", url(""));
$("body").css("background-image", 'url("https://placehold.it/600")');
mctwodigitzero sends brownie points to @skyc0der :sparkles: :thumbsup: :sparkles:
:star2: 2010 | @skyc0der |http://www.freecodecamp.com/skyc0der
$("body").css({
"background-image": 'url("https://placehold.it/600")',
"background-position": "center center",
"background-size": "cover",
"min-height": "100vh"
});
center center
?
function mutation(arr) {
var lcArr = arr.toString().toLowerCase().split(",");
return lcArr[0].indexOf(lcArr[1]) !== -1;
}
mutation(["hello", "Hello"]);
Rule #1: Don't look at the example project's code. Figure it out for yourself.
var weatherIcon = {
"Clear": Skycons.CLEAR_DAY,
"Partly Cloudy": Skycons.PARTLY_CLOUDY_DAY,
"Mostly Cloudy": Skycons.CLOUDY,
"Rain": Skycons.RAIN,
"Light Rain": Skycons.RAIN,
"Light Thunderstorms and Rain": Skycons.RAIN,
"Sleet": Skycons.SLEET,
"Snow": Skycons.SNOW,
"Windy": Skycons.WIND,
"Overcast": Skycons.CLOUDY,
"Heavy Rain": Skycons.RAIN
};
skycons.add('document.getElementById(iconDiv), weatherIcon[weatherCondition]);
snow
you do weatherIcon[weatherSummary]
and it will look up the value for snow in the table. That can be a background image
"snow"
then weatherIcon[weatherSummary=>"snow"]
mctwodigitzero sends brownie points to @skyc0der :sparkles: :thumbsup: :sparkles:
:star2: 2011 | @skyc0der |http://www.freecodecamp.com/skyc0der
weatherIcon[json["current"].condition.text]
text==="Clear" then it will select "Clear"'s property from weatherIcon
Object{
ns: 0,
size:34792,
snippet: "java applet is a small application",
timestamp: "2017 -05-30T",
title:"java servlet"
wordcount: 1610,
}
jasonrest sends brownie points to @hemakshis :sparkles: :thumbsup: :sparkles:
:cookie: 280 | @hemakshis |http://www.freecodecamp.com/hemakshis
function mutation(arr) {
var firstLcArr = arr[0].toLowerCase();
var secLcArr = arr[1].toLowerCase();
for (i = 0; i < secLcArr.length; i++) {
if (firstLcArr.indexOf(secLcArr[i]) < 0) {
return false;
}
return true;
}
}
mutation(["hello", "hey"]);
lumexralph sends brownie points to @kelechichinaka :sparkles: :thumbsup: :sparkles:
:cookie: 290 | @kelechichinaka |http://www.freecodecamp.com/kelechichinaka
:bulb: to format code use backticks! ``` more info
Array.prototype.reduce.call
:var uniqueInOrder=function(iterable){
return Array.prototype.reduce.call(iterable, function(a,b) {
if (a[a.length-1] !== b) a.push(b);
return a;
}, []);
}
uniqueInOrder('AAAABBBCCDAABBB'); // should return ['A', 'B', 'C', 'D', 'A', 'B']
m-c-haker sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6212 | @manish-giri |http://www.freecodecamp.com/manish-giri
arr[0]
(firstLcArr.indexOf(secLcArr[i]) === -1 )
that's to say it found it so
.call()
method in this example: :point_up: July 8, 2017 12:43 PM
call
is a method of the Function
object, used to call a function on an iterable/object that doesn't have that method
.split()
the strings?
.reduce()
method to work with a string?
function mutation(arr) {
var firstLcArr = arr[0].toLowerCase();
var secLcArr = arr[1].toLowerCase();
for (i = 0; i < secLcArr.length; i++) {
if (firstLcArr.indexOf(secLcArr[i]) === -1) {
return false;
}
}
return true;
}
mutation(["Hello", "hello"]);
string
and array
are similar, they're both iterable and they're both sequences of characters and elements
return true
outside the for loop.
m-c-haker sends brownie points to @manish-giri and @lumexralph :sparkles: :thumbsup: :sparkles:
:warning: m-c-haker already gave manish-giri points
:star2: 2021 | @lumexralph |http://www.freecodecamp.com/lumexralph
```
function mutation(arr) {
var firstLcArr = arr[0].toLowerCase();
var secLcArr = arr[1].toLowerCase();
for (i = 0; i < secLcArr.length; i++) {
if (firstLcArr.indexOf(secLcArr[i]) < 0) {
return false;
}
}
return true;
}
mutation(["hello", "hey"]);
reduce
work
function mutation(arr) {
return arr[1].toLowerCase()
.split('')
.every(function(letter) {
return arr[0].toLowerCase()
.indexOf(letter) != -1;
});
}
.every()
returns true if the callback you pass to it, returns true for every element in the array you're using .every()
on @M-C-Haker
[2, 4, 6].every(num => num % 2 === 0);
iterable
, it wouldn't work because iterable
is a string
:return iterable.reduce(function(a,b) {
if (a[a.length-1] !== b) a.push(b);
return a;
}, []);
[0, 1, 2, 3, 4].reduce(
function (
accumulator,
currentValue,
currentIndex,
array
) {
return accumulator + currentValue;
}
);
//The callback would be invoked four times, with the arguments and return values in each call being as follow
m-c-haker sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:warning: m-c-haker already gave manish-giri points
reduce()
works, what I haven't grasped is the call()
method
call
it uses a function on the this
object, it only got the function from the Array.prototype
object
reduce
should not work on string
but call, apply
method gave that power to us, call
is a method of the Function
object, even when reduce
was created for array, it still is a function, that made it possible to be able to use call
on it,
call
did was just allowed you to be able to use reduce
on the string
longnt80 sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:star2: 2022 | @lumexralph |http://www.freecodecamp.com/lumexralph
OOP
for my pomodoro clock
this
in React is different from this
in vanilla JS?
this
behaves the same way as you would expect to, with regular JS classes
get
is just the pattern for creating a getter in JS
@Manish-Giri ahh tnx
hudge gaps in my knowledge
giveback007 sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6213 | @manish-giri |http://www.freecodecamp.com/manish-giri
get
in react apps
Nesting for Loops
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for(var i=0;i<arr.length;i++){
for (var j=0; j < arr[i].length; j++){
product*=arr[i][j];
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
haha i finished the challenge but boy wtf just happened i don't get it is anyone care to explain
$('#output').prepend("<li><a href=" "wikipedia.com/wiki/" + test[i].title + ">" + test[i].title + "</a><br/><span>" + test[i].snippet +"</span></li>");
" "wikipedia.com/wiki/"
$('#output').prepend("<li><a href=" wikipedia.com/wiki/ + test[i].title + ">" + test[i].title + "</a><br/><span>" + test[i].snippet +"</span></li>");
$("#locate").html(city); '''. Hello! Is this code well writted?? I cant get the locatión name ....
:bulb: to format code use backticks! ``` more info
$("#locate").html(city); '''
:bulb: to format code use backticks! ``` more info
$("#locate").html(city); ```. Sorry i am with the mobile.....
var gooloc ="https://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat + "," + long +"&key=AIzaSyBUzuWvyNu66-dU5DW8DfMqNQ87JgFPCUs";
<form>
and a submit button. To start the search. Or will that mean that the page will be reloaded?
makalohri sends brownie points to @knrt10 :sparkles: :thumbsup: :sparkles:
:cookie: 451 | @knrt10 |http://www.freecodecamp.com/knrt10
.name-container{
display: inline-block;
border-bottom: 1px dotted black;
margin-top: -10px;
font-size: 15px;
//background-color: pink;
}
span.char3:hover{
transform: translateX(0);
}
span.char2:hover{
transform: translateX(0);
}
span.char4:hover{
transform: translateX(0);
}
span.char5:hover{
transform: translateX(0);
}
span.char6:hover{
transform: translateX(0);
}
}
span{
display: inline-block;
margin-left: 0.5px;
}
span.char2{
transform: translateX(3.54em);
transition: transform 500ms;
}
span.char3{
transform: translateX(0.34em);
transition: transform 500ms;
}
span.char4{
transform: translateX(0.32em);
transition: transform 500ms;
}
span.char5{
transform: translateX(0.32em);
transition: transform 500ms;
}
span.char6{
transform: translateX(-3.21em);
transition: transform 500ms;
}
$(document).ready(function(){
$('#search-btn').click(function(){
var keyword =$('#keyword').val();
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+keyword+"&format=json&callback=?";
//api call
$.ajax({
type: "GET",
url: url,
async: false,
dataType: "json",
success: function(data){
//retriving data
for(var i= 0; i<data[1].length; i++){
//sending markups to the ul tag with data
$('#results').prepend("<li><a href="+data[3][i]+">"+data[1][i]+"</a><p>"+data[2][i]+"</p></li>");
}
},
error: function(errorMessage){
//in-case of error
alert("error");
}
});
});
});
The wikipedia project.e.preventDefault();
below $('#search-btn').click(function(e){
joetinnyspace sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4467 | @chrono79 |http://www.freecodecamp.com/chrono79
@Chrono79 why isn't the
for(var i= 0; i<data[1].length; i++){
// sending markups to the ul tag with data
$('#results').prepend("<li><a href="+data[3][i]+">"+data[1][i]+"</a><p>"+data[2][i]+"</p></li>");
}
doesn't work, after this $('#results').htmll('');
for (i = 0; i<10; i++)
?
for ([initialization]; [condition]; [final-expression])
statement
if (a < b)
you don't put a semicolon inside those parentheses either
michielhuijse sends brownie points to @chrono79 and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8011 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
:star2: 4468 | @chrono79 |http://www.freecodecamp.com/chrono79
for (i = 0, i<10, i++;)
:D
var i = 0;
while (i < 10) {
<body>
i++;
}
for(int num:numbers)
vivekranjan92 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4469 | @chrono79 |http://www.freecodecamp.com/chrono79
Hello everybody! Can anyone explain me why does my code not work?
function truthCheck(collection, pre) {
// Is everyone being true?
collection.forEach(function(element) {
if (pre in element === false)
return false;
});
return true;
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
If every object contains second argument ("sex") function should return true else false.
var myArr1 = [2, 2, 2, 2, 2];
var newArr = myArr1.reduce(
function(val, idx) {
console.log(idx);
console.log(val);
return myArr1[idx] += val;
});
console.log(newArr);
typeof NaN
"number"
typeof NaN
is a number
georgeii sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8012 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
console.log("test")
test
undefined
sanket143 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8013 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
function(acc, val) {
console.log(acc);
console.log(val);
return acc + val;
}
waingor sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8014 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Profile Lookup
We have an array of objects representing different people in our contacts lists.
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.
The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If firstName does not correspond to any contacts then return "No such contact"
If prop does not correspond to any valid properties then return "No such property"
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for(i=0;i<contacts.length;i++){
if (firstName,prop==contacts.firstName){
return contacts;
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
How should i approach this challenge i am confused as fuck when it comes to loops and object
contacts
is an array, so you have to use array indexing in some form to access the objects within the array. You are generating the index with your variable i
, but you aren't using it.
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for(i=0;i<contacts.length;i++){
if (firstName==contacts[i].firstName){
return contacts[i];
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
munkhmanlai sends brownie points to @kr5hn4 :sparkles: :thumbsup: :sparkles:
:cookie: 199 | @kr5hn4 |http://www.freecodecamp.com/kr5hn4
course++;
is not working ?? https://codepen.io/gr8ayu/pen/yXRLep?editors=1010
munkhmanlai sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 3081 | @khaduch |http://www.freecodecamp.com/khaduch
@khaduch
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for(i=0;i<contacts.length;i++){
if (firstName==contacts[i].firstName){
return contacts[i].firstName;
}else return "No such contact";
if (prop==contact[i].likes){
return contacts[i].likes;
}else return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
it doesn't giving me likes or the prop?
.pass
and none for .fail
- so when you click, it increments and then decrements your value and updates it twice. (I see that someone answered while I wasn't looking... :) )
contact[i].likes
this result of this expression is a property value.gr8ayu sends brownie points to @sjames1958gm and @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 3082 | @khaduch |http://www.freecodecamp.com/khaduch
:star2: 8015 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
amitp88 sends brownie points to @shivamg11000 :sparkles: :thumbsup: :sparkles:
:cookie: 370 | @shivamg11000 |http://www.freecodecamp.com/shivamg11000
vivekranjan92 sends brownie points to @gr8ayu :sparkles: :thumbsup: :sparkles:
:cookie: 332 | @gr8ayu |http://www.freecodecamp.com/gr8ayu
function titleCase(str) {
str = str.split(" ");
var arraySize =str.length;
var arrayStr;
var newStr = "";
for (var i = 0; i < arraySize; i++){
arrayStr = str[i].charAt(0).toUpperCase() + str[i].substr(1).toLowerCase();
newStr = newStr + " " +(str[i].charAt(0).toUpperCase() + str[i].substr(1).toLowerCase()).toString();
}
return newStr;
}
titleCase("I'm a little tea pot");
gr8ayu sends brownie points to @skyc0der :sparkles: :thumbsup: :sparkles:
:star2: 2012 | @skyc0der |http://www.freecodecamp.com/skyc0der
fyudi sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8016 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
export default class Timeline extends Component {
constructor(props) {
super();
this.arr = this.generateTimelineArray();
this.state = {el : 'empty'};
}
componentDidMount() {
// it is a regular DOM node
this.setState({
el: this.refs.el
});
console.log('------------------');
console.log(this.state.el)
}
render() {
const timeList = list.map((element) =>
<TimelineElement element={element}/>
);
return (
<div className="timeline__container" >
<div className="timeline__container--line" ref="el" >
</div>
{timeList}
</div>
);
}
How could i get timeline__container--line ref to be set in the state ?
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if(checkObj.hasOwnProperty(checkProp)){
return myObj(checkProp);
}else
return "Not found";
}
// Test your code by modifying these values
checkObj("gift");
if(checkObj.hasOwnProperty(checkProp)){
<- this line is wrong
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj(checkProp);
}else
return "Not found";
}
// Test your code by modifying these values
checkObj("gift");
"Not Found"
, as "Not found"
(lower-case 'f') won't pass.2895388807 sends brownie points to @munkhmanlai :sparkles: :thumbsup: :sparkles:
:cookie: 236 | @munkhmanlai |http://www.freecodecamp.com/munkhmanlai
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj(checkProp);
}else
return "Not found";
}
// Test your code by modifying these values
checkObj("gift"
does anyone know wtf is wrong with this
gear += addGear
According to your code :
myBike.setGear(1)
myBike.getGear() // evaluates to 1
myBike.setGear(4)
myBike.getGear() // evaluates to 5
myBike.setGear(3)
myBike.getGear() // evaluates to 8
Instead it should be :
myBike.setGear(1)
myBike.getGear() // evaluates to 1
myBike.setGear(4)
myBike.getGear() // evaluates to 4
myBike.setGear(3)
myBike.getGear() // evaluates to 3
iggysmith sends brownie points to @thomlom :sparkles: :thumbsup: :sparkles:
:cookie: 496 | @thomlom |http://www.freecodecamp.com/thomlom
var json =
[
{
"id": 0,
"imageLink": "https://s3.amazonaws.com/freecodecamp/funny-cat.jpg",
"altText": "A white cat wearing a green helmet shaped melon on it's head. ",
"codeNames": [
"Juggernaut",
"Mrs. Wallace",
"Buttercup"
]
},
{
"id": 1,
"imageLink": "https://s3.amazonaws.com/freecodecamp/grumpy-cat.jpg",
"altText": "A white cat with blue eys, looking very grumpy. ",
"codeNames": [
"Oscar",
"Scrooge",
"Tyrion"
]
},
{
"id": 2,
"imageLink": "https://s3.amazonaws.com/freecodecamp/mischievous-cat.jpg",
"altText": "A ginger cat with one eye closed and mouth in a grin-like expression. Looking very mischievous. ",
"codeNames": [
"The Doctor",
"Loki",
"Joker"
]
}
];
var Y = json.reduce(function(accumulator,currentvalue) {
return "<div><img src='" + accumulator + currentvalue.imageLink;
// Only change code above this line.
},"");
console.log(Y);
var runIt = function (a) {
console.log(a);
a();
}
Could somebody explain to me what is this (a) here, as far as I understand it is a parameter, but I hear somebody saying it is a passed value, even calling it a variable, i think name maynot make that big difference but still want to make it clear. Also, I don't get it why a() is used, it says to execute the function but I think that why not just use paranthesis.
challenger11 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8017 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
waingor sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8018 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
@sjames1958gm lets say,
function someThing (a) {
console.log (a);
a();
}
will it still be like this?
a();
if the passed value is a function.
someThing(function() { })'
Or
function x() {
}
someThing(x);
In both cases the function will still do a()
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for(i=0;i<contacts.length;i++){
if (firstName==contacts[i].firstName){
return contacts[i].firstName;
}else return "No such contact";
if (prop==contact[i].hasOwnProperty(prop)){
return contacts[i].prop;
}else return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
challenger11 sends brownie points to @saifsmailbox98 and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:warning: challenger11 already gave sjames1958gm points
:cookie: 738 | @saifsmailbox98 |http://www.freecodecamp.com/saifsmailbox98
firstName
or"No such contact"
, by checking the first contact object only.
firstName
prop
prop==contact[i].hasOwnProperty(prop)
will be false always for that arrayif(x==1 && y==2) { //do something}
var myObj = {
"x": 50,
"y": 40
}
myObj.hasOwnProperty("x"); //true
myObj.hasOwnProperty("y"); //true
myObj.hasOwnProperty("z"); //false
gadzet sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8019 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
array.reduce(function(currentval, index) {
}
waingor sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4470 | @chrono79 |http://www.freecodecamp.com/chrono79
array.reduce(function(accum, currentval, currentindex) {
}
//初始化变量
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// 请把你的代码写在这条注释以下
while(contacts.hasOwnProperty(firstName)){
return contacts[firstName];
}return "No such contact";
while(contacts.hasOwnProperty(prop)){
return contacts[prop];
}return "No such property";
// 请把你的代码写在这条注释以上
}
// 你可以修改这一行来测试你的代码
lookUp("Akira", "likes");
I am doing this problem https://projecteuler.net/problem=12 and I made a function on how many divisors a number have like 28 have 1,2,4,7,14,28.
function divs(n) { //28 => 6
let arr = [1];
for (let i = 2; i <= n/2; i++) {
if (n % i === 0) {
arr.push(i);
}
}
arr.push(n);
return arr.length;
}
The problem is that my function is not fast enough. I found this function
function divs(n) {
let nod = 0;
let sqrt = Math.sqrt(n);
for (let i = 1; i <= sqrt; i++) {
if (n % i === 0) {
nod += 2;
}
}
if (sqrt * sqrt === n) {
nod--;
}
return nod;
}
I dont understand why it uses sqrt(n) and nod += 2. Can someone help me understand this?
Hello! My string should include only numbers or brackets or dashes or spaces. Can you help me with RegExp?
var reg = /[\s\d)(-]/;
I want to use test()
function for every character of my string but I can't declare RegExp correctly
What is the value of the first triangle number to have over five hundred divisors?
/[\s\d\(\)\-\[\]]/g
@Chrono79 Thanks. @Stuhl Well, my divs() function runs okay but all my code to work on that problem in project euler is not fast
function nthLayer(n) {
return .5 * n * (n + 1);
}
// function divs(n) { //28 => 6
// let arr = [1];
// for (let i = 2; i <= n/2; i++) {
// if (n % i === 0) {
// arr.push(i);
// }
// }
// arr.push(n);
// return arr.length;
// }
function divs(n) {
let nod = 0;
let sqrt = Math.sqrt(n);
for (let i = 1; i <= sqrt; i++) {
if (n % i === 0) {
nod += 2;
}
}
if (sqrt * sqrt === n) {
nod--;
}
return nod;
}
function divsNeeded(m) {
let n = 1
while (divs(nthLayer(n)) < m) {
n++;
}
return nthLayer(n);
}
console.log(divsNeeded(501));
I need to use another method. I can get some hits here: http://www.mathblog.dk/triangle-number-with-more-than-500-divisors/
hypercuber sends brownie points to @chrono79 and @stuhl :sparkles: :thumbsup: :sparkles:
:cookie: 169 | @stuhl |http://www.freecodecamp.com/stuhl
:star2: 4471 | @chrono79 |http://www.freecodecamp.com/chrono79
controller
it's just component)
Let;s assume you have nested components like
<list>
<list-child from-parent="listItem"></list-child>
</list>
If you enable one way binding for list-child
no matter what you do with listItem
in list-child
component, the reference of it in list
will not be affected
list-child
to update listItem
which is referred to as fromParent
in your list-child
component, whatever changes you make to fromParent
will reflect in listItem
of list
component
one-way-binding
: component will be updated with data as soon as there is new data. If you type something in the component(input) your data will not update.two-way-binding
: component will be updated with data as soon as there is new data. If you type something in the component (input) your data will be updated with the input data.
one-way
means your data goes only one way - to the element but not back
georgeii sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4472 | @chrono79 |http://www.freecodecamp.com/chrono79
function nthLayer(n) {
return .5 * n * (n + 1);
}
function divs(n) { //28 => 6
let arr = [1];
for (let i = 2; i <= Math.sqrt(n); i++) { //what if it is perfect sq ***
if (n % i === 0) {
arr.push(i);
}
}
arr.push(n);
return arr.length;
}
// function divs(n) { //yt || ask why this work ***
// let nod = 0;
// let sqrt = Math.sqrt(n);
// for (let i = 1; i <= sqrt; i++) {
// if (n % i === 0) {
// nod += 2;
// }
// }
// if (sqrt * sqrt === n) {
// nod--;
// }
// return nod;
// }
function divsNeeded(m) {
let n = 1;
while (divs(nthLayer(n)) < m) {
n++;
}
return nthLayer(n);
}
console.log(divsNeeded(501));
gadzet sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 100 | @sweetcodinginc |http://www.freecodecamp.com/sweetcodinginc
this.data
- use the state instead. Also, if you want to pass down a function which references its own context, you should bind it to the component in the constructor like:this.getLineOffset = this.getLineOffset.bind(this);
this
in your function refers to the child component (The calling component) which would be Timeline
. Timeline has no variable data
defined.
gadzet sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1244 | @alpox |http://www.freecodecamp.com/alpox
this.getLineOffset = this.getLineOffset();
gadzet sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:warning: gadzet already gave sweetcodinginc points
undefined
.this.getLineOffset = this.getLineOffset.bind(this);
getLineOffset
at that line
this
gadzet sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:warning: gadzet already gave sweetcodinginc points
export default class Timeline extends Component {
componentWillMount(){
console.dir("willMount",this.refs.el);
}
componentDidMount() {
console.dir("didMount",this.refs.el)
this.props.sendData(this.refs.el.offsetLeft);
}
render() {
return <div className="timeline__container--line" ref="el" />;
}
}
if u check console... "willmount" shows empty (element is not rendered yet.. no "el" component exists )
gadzet sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3477 | @moigithub |http://www.freecodecamp.com/moigithub
Make sure input can be evaluated
Evaluate input expression on click of "="
function myLocalScope() {
var myVar =5;
console.log(myVar);
}
myLocalScope();
console.log(myVar);
Number(7(newline)8) + Number(3 )
derekbmcintire sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3478 | @moigithub |http://www.freecodecamp.com/moigithub
function destroyer(arr) {
var finalArr=[];
var itemsLeft=arr[0].length;
for(var items=0;items<=itemsLeft;items++){
for(var numSearch=arr.slice(1).length;numSearch>1;numSearch--){
var isAMatch=arr.match(arr[numSearch-1] /g);
if(isAMatch){break;}else{
finalArr.push(arr[items]);
break;
}
}
}
return finalArr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
named parameter
arr[1, 2, 3, 1, 2, 3]
arguments
objectwilliamtech814 sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3479 | @moigithub |http://www.freecodecamp.com/moigithub
var first = function(n) {
return n[0];
}
console.log(first([7, 9, 0, -2],3));
Hello! This function outputs first element of an array. In the case above the result is 7. However, I am told that I am supposed to get [7, 9, 0]
instead. What maybe a problem?
@serhiicss Can you post the problem text? There are a few ways to return 7,9,0
based on that input, for example:
var first = function(n) {
return n.slice(0,arguments[1]);
}
console.log(first([7, 9, 0, -2],3));
but it depends on expected input to the first
function.
var first = function(n) {
return n.slice(0, 3);
}
console.log(first([7, 9, 0, -2]));
function threeInOne(arr) {
var output = [], copy;
copy = arr.slice();
copy = copy.splice(0, 3);
copy = copy.reduce( function (a, b) {
return a + b
},0)
output.push(copy);
return output;
}
var first = function(arr, n) {
return arr.splice(0, n || 1);
};
can anyone tell me why these links arent getting back to the wiki pages?
$('#output').prepend("<li><a href='wikipedia.com/wiki/" + test[i].title + "'>" + test[i].title + "</a>" + test[i].snippet +"</li>");
function destroyer(arr) {
var testNum=arguments.length-1;
var finalArr=arr.filter(
function(item){
for(var i=1;i<=testNum;i++){
if(item==arguments[i]){return false;}
}
return true;
});
return finalArr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
https://
"<li><a href='https://en.wikipedia.org/wiki/"
jasonrest sends brownie points to @skyc0der :sparkles: :thumbsup: :sparkles:
:star2: 2021 | @skyc0der |http://www.freecodecamp.com/skyc0der
$(#locate).html(province)
dosent change my location in the html..... i tried outside the getJSON function and it works (but i dont hace the variables..., i can not get result inside getJSON ,even "text" ..... i can not change my locate text in html.... anyone can help me on this??